本地化库
本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。
平面类别
在字符编码间转换,包括 UTF-8、UTF-16、UTF-32
std::codecvt
template< class InternT, |
类 std::codecvt 封装字符串的转换,包括宽和多字节,从一种编码到另一种。通过 std::basic_fstream<CharT> 进行的所有 I/O 操作都使用流中感染的 std::codecvt<CharT, char, std::mbstate_t> 本地环境平面。
继承图
标准库提供以下独立(本地环境无关)特化:
定义于头文件 | |
std::codecvt<char, char, std::mbstate_t> | 恒等转换 |
std::codecvt<char16_t, char, std::mbstate_t> | 在 UTF-16 和 UTF-8 间转换 (C++11 起)(C++20 中弃用) |
std::codecvt<char16_t, char8_t, std::mbstate_t> | 在 UTF-16 和 UTF-8 间转换 (C++20 起) |
std::codecvt<char32_t, char, std::mbstate_t> | 在 UTF-32 和 UTF-8 间转换 (C++11 起)(C++20 中弃用) |
std::codecvt<char32_t, char8_t, std::mbstate_t> | 在 UTF-32 和 UTF-8 间转换 (C++20 起) |
std::codecvt<wchar_t, char, std::mbstate_t> | 在系统原生宽和单字节窄字符集间转换 |
另外, C++ 程序中构造每个的 locale 对象实现其自身的四个特化的( locale 限定)版本。
成员类型
成员类型 | 定义 |
intern_type | InternT |
extern_type | ExternT |
state_type | State |
调用 do_encoding & 返回产生一个 internT 字符所需的 externT 字符数,若此值为常数
std::codecvt<InternT,ExternT,State>::encoding, do_encoding
public: | (1) | (C++11 前) |
public: | (C++11 起) | |
protected: | (2) | (C++11 前) |
protected: | (C++11 起) |
1) 公开成员函数,调用最终导出类的成员函数 do_encoding
。
2) 若此 codecvt 平面所表示的编码映射每个内部字符为相同的常数个外部字符,则返回该常数。若编码为变长(如 UTF-8 或 UTF-16 ),则返回 0 。若编码依赖平台,则返回 -1 。
返回值
对应一个 internT
字符的准确 externT
字符数。若该数变化则为 0 ,若编码依赖平台则为 -1 。
不转换特化 std::codecvt<char, char, std::mbstate_t> 返回 1 。
调用示例 linux
#include <locale>
#include <iostream>
int main()
{
std::cout << "en_US.utf8 is a variable-length encoding, encoding() returns "
<< std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
std::locale("en_US.utf8")
).encoding() << std::endl;
std::cout << "zh_CN.gb18030 is also variable-length, encoding() == "
<< std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
std::locale("zh_CN.gb18030")
).encoding() << std::endl;
std::cout << "ru_RU.koi8r is a single-byte encoding encoding() == "
<< std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
std::locale("ru_RU.koi8r")
).encoding() << std::endl;
return 0;
}
输出
系统环境
Linux localhost.localdomain 2.6.32-431.el6.x86_64
#1 SMP Fri Mar 28 08:38:24 CST 2014 x86_64 x86_64 x86_64 GNU/Linux
语言环境
LANG=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
en_US.utf8 is a variable-length encoding, encoding() returns 0
zh_CN.gb18030 is also variable-length, encoding() == 0
ru_RU.koi8r is a single-byte encoding encoding() == 1
Press <RETURN> to close this window...
调用示例linux
#include <locale>
#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>
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;
}
int main()
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);
for (std::vector<std::wstring>::const_iterator str = locals.begin();
str != locals.end(); ++str)
{
std::wcout << *str ;
std::cout << " is a variable-length encoding, encoding() returns "
<< std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
std::locale(stows(*str))
).encoding() << std::endl;
}
return 0;
}
输出
de-DE_phoneb is a variable-length encoding, encoding() returns 0
es-ES_tradnl is a variable-length encoding, encoding() returns 0
hu-HU_technl is a variable-length encoding, encoding() returns 0
ja-JP_radstr is a variable-length encoding, encoding() returns 0
ka-GE_modern is a variable-length encoding, encoding() returns 0
x-IV_mathan is a variable-length encoding, encoding() returns 0
zh-CN_phoneb is a variable-length encoding, encoding() returns 0
zh-CN_stroke is a variable-length encoding, encoding() returns 0
zh-HK_radstr is a variable-length encoding, encoding() returns 0
zh-MO_radstr is a variable-length encoding, encoding() returns 0
zh-MO_stroke is a variable-length encoding, encoding() returns 0
zh-SG_phoneb is a variable-length encoding, encoding() returns 0
zh-SG_stroke is a variable-length encoding, encoding() returns 0
zh-TW_pronun is a variable-length encoding, encoding() returns 0
zh-TW_radstr is a variable-length encoding, encoding() returns 0