本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。
平面类别
定义 std::money_get 与 std::money_put 所用的货币格式解析器的参数
std::moneypunct
template< class CharT, bool International = false > |
平面 std::moneypunct 封装货币值格式化偏好。流 I/O 操纵符 std::get_money 和 std::put_money 通过 std::money_get 和 std::money_put 用 std::moneypunct 分析货币值输入及格式化货币值输出。
继承图
标准库提供四个孤立(独立于本地环境)的特化:
定义于头文件 | |
std::moneypunct<char> | 提供 "C" 本地环境偏好的等价版本 |
std::moneypunct<wchar_t> | 提供 "C" 本地环境偏好的宽字符等价版本 |
std::moneypunct<char, true> | 提供 "C" 本地环境偏好的等价版本,带国际通货符号 |
std::moneypunct<wchar_t, true> | 提供 "C" 本地环境偏好的宽字符等价版本,带国际通货符号 |
另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。
成员类型
成员类型 | 定义 |
char_type | CharT |
string_type | std::basic_string<CharT> |
构造新的 moneypunct 平面
std::moneypunct<CharT,International>::moneypunct
explicit moneypunct( std::size_t refs = 0 ); |
创建 std::moneypunct 平面并转发引用计数 refs
到基类构造函数 locale::facet::facet() 。
参数
refs | - | 开始的引用计数 |
销毁 moneypunct 平面
std::moneypunct<CharT,International>::~moneypunct
protected: ~moneypunct(); |
析构 std::moneypunct 平面。此析构函数为受保护且为虚(由于基类析构函数为虚)。 std::moneypunct 类型对象,同大多数平面,只能在最后一个实装此平面的 std::locale 离开作用域时,或若用户定义导出自 std::moneypunct 并实现公开构造函数,才会被销毁。
调用示例
#include <iostream>
#include <locale>
struct Destructible_moneypunct : public std::moneypunct<wchar_t>
{
Destructible_moneypunct(std::size_t refs = 0) : moneypunct(refs) {}
// 注意:隐式析构函数为公开
};
int main()
{
Destructible_moneypunct dc;
// std::moneypunct<wchar_t> c; // 编译错误:受保护析构函数
return 0;
}
提供用作小数点的字符
std::moneypunct<CharT,International>::decimal_point,
std::moneypunct<CharT,International>::do_decimal_point
public: | (1) | |
protected: | (2) |
1) 公开成员函数,调用最终导出类的成员函数 do_decimal_point
。
2) 返回用作货币 I/O 中小数点的字符,若格式使用小数(即若 do_frac_digits() 大于零)。对于典型的美国本地环境,其为字符 '.' (或 L'.' )。
返回值
保有小数点的 CharT
类型值。
调用示例 windows
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <exception>
#include <Windows.h>
std::vector<std::wstring> locals;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
locals.push_back(pStr);
return TRUE;
}
std::string stows(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
void show_dpt(const char* locname)
{
std::locale locale(locname);
std::cout.imbue(locale);
std::cout << locale.name() << " decimal point is '"
<< std::use_facet<std::moneypunct<char>>(locale).decimal_point()
<< "' for example: " << std::showbase << std::put_money(123);
if (std::use_facet<std::moneypunct<char>>(locale).frac_digits() == 0)
{
std::cout << " (does not use frac digits) ";
}
std::cout << std::endl;
}
int main()
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);
for (std::vector<std::wstring>::const_iterator str = locals.begin();
str != locals.end(); ++str)
{
if (stows(*str) == "x-IV_mathan")
{
continue;
}
show_dpt(stows(*str).c_str());
}
return 0;
}
输出
de-DE_phoneb decimal point is ',' for example: 1,23 €
es-ES_tradnl decimal point is ',' for example: 1,23 €
hu-HU_technl decimal point is ',' for example: 1,23 Ft
ja-JP_radstr decimal point is '.' for example: \123 (does not use frac digits)
ka-GE_modern decimal point is ',' for example: 1,23 ?
zh-CN_phoneb decimal point is '.' for example: ¥1.23
zh-CN_stroke decimal point is '.' for example: ¥1.23
zh-HK_radstr decimal point is '.' for example: HK$1.23
zh-MO_radstr decimal point is '.' for example: MOP1.23
zh-MO_stroke decimal point is '.' for example: MOP1.23
zh-SG_phoneb decimal point is '.' for example: $1.23
zh-SG_stroke decimal point is '.' for example: $1.23
zh-TW_pronun decimal point is '.' for example: NT$1.23
zh-TW_radstr decimal point is '.' for example: NT$1.23