如何获取我的环境的当前区域设置
How to get current locale of my environment?
曾在Linux中尝试过以下代码,但在不同的LANG
设置下总是返回"C"。
#include <iostream>
#include <locale.h>
#include <locale>
using namespace std;
int main()
{
cout<<"locale 1: "<<setlocale(LC_ALL, NULL)<<endl;
cout<<"locale 2: "<<setlocale(LC_CTYPE, NULL)<<endl;
locale l;
cout<<"locale 3: "<<l.name()<<endl;
}
$ ./a.out
locale 1: C
locale 2: C
locale 3: C
$
$ export LANG=zh_CN.UTF-8
$ ./a.out
locale 1: C
locale 2: C
locale 3: C
我应该怎么做才能在Linux(比如Ubuntu)中获得当前的区域设置?
另一个问题是,在Windows中获取区域设置的方式是否相同?
来自man 3 setlocale
(新格言:"如果有疑问,请阅读整个手册页。"):
如果区域设置为
""
,则应根据环境变量设置区域设置的每个部分。
因此,我们可以通过在程序开始时调用setlocale
来读取环境变量,如下所示:
#include <iostream>
#include <locale.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
return 0;
}
我的系统不支持zh_CN
区域设置,如下输出所示:
$/a.outLC_ALL:en_US.utf8LC_CTYPE:en_US.utf8$export LANG=zh_CN.UTF-8$/a.outLC_ALL:CLC_CTYPE:C
Windows:我对Windows区域设置一无所知。我建议从MSDN搜索开始,如果您还有问题,请打开单独的堆栈溢出问题。
刚刚学会了如何通过C++获得语言环境,只需使用一个空字符串"来构造std::locale,它与setlocale(LC_ALL,")做同样的事情。
locale l("");
cout<<"Locale by C++: "<<l.name()<<endl;
此链接描述了C语言环境和C++语言环境之间的细节差异。
对于Windows,请使用以下代码:
LCID lcid = GetThreadLocale();
wchar_t name[LOCALE_NAME_MAX_LENGTH];
if (LCIDToLocaleName(lcid, name, LOCALE_NAME_MAX_LENGTH, 0) == 0)
error(GetLastError());
std::wcout << L"Locale name = " << name << std::endl;
这将打印类似于"en-US"的内容。
要清除子语言信息,请使用以下方法:
wchar_t parentLocateName[LOCALE_NAME_MAX_LENGTH];
if (GetLocaleInfoEx(name, LOCALE_SPARENT, parentLocateName, LOCALE_NAME_MAX_LENGTH) == 0)
error(GetLastError());
std::wcout << L"parentLocateName = " << parentLocateName << std::endl;
这只会给你一个"en"。
一个很好的替代std::locale的选择是boost::locale,它能够返回更可靠的信息-请参阅http://www.boost.org/doc/libs/1_52_0/libs/locale/doc/html/locale_information.html
boost::locale::info具有以下成员函数:
std::string name() -- the full name of the locale, for example en_US.UTF-8
std::string language() -- the ISO-639 language code of the current locale, for example "en".
std::string country() -- the ISO-3199 country code of the current locale, for example "US".
std::string variant() -- the variant of current locale, for example "euro".
std::string encoding() -- the encoding used for char based strings, for example "UTF-8"
bool utf8() -- a fast way to check whether the encoding is UTF-8.
std::locale
的默认构造函数创建全局C++区域设置的副本。
因此,要获得当前区域设置的名称:
std::cout << std::locale().name() << 'n';
相关文章:
- 在 Linux 中存储区域设置名称的缓冲区大小应该是多少?
- 提升区域设置规范化带状字符,但不规范化重音
- 是否有任何区域设置会影响宽字符编码?
- 提升获取文本的区域设置"Conversion failed"
- 提升 1.41.0 : 提升::区域设置替代方案?
- std::locale 向"en_US.UTF-8"区域设置抛出runtime_error异常
- 默认情况具有哪个区域设置
- 区域设置是否有定义负号的方面?
- QT 将文本翻译成特定的区域设置
- 我可以强制 QFileDialog 使用系统区域设置而不是系统语言吗?
- 如何调用 GetAltMonthNames 来填充外部区域设置月份字符串的安全数组
- 提升区域设置不完整类型boundary_indexing<char32_t>
- 默认区域设置"root"在 ICU 中如何工作?
- std::regex总是可以识别区域设置吗?
- std::string 的运算符<是否应该受到当前区域设置的影响?
- C++为任何区域设置区域设置
- 如何显示与系统不同的区域设置的QDate月
- C++获取用户首选的编码或区域设置编码
- 使用cstring.format()中区域设置的小数分离器
- 当区域设置需要"3,14"时,如何使用scanf解析像"3.14"这样的数字