C++ 如何获取我的环境的当前语言环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12170488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:57:54  来源:igfitidea点击:

How to get current locale of my environment?

c++clocale

提问by Deqing

Had tried following code in Linux, but always return 'C' under different LANGsettings.

曾在 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

What should I do to get current locale setting in Linux(like Ubuntu)?

我应该怎么做才能在 Linux(如 Ubuntu)中获得当前的语言环境设置?

Another question is, is it the same way to get locale in Windows?

另一个问题是,在 Windows 中获取语言环境的方式是否相同?

采纳答案by Dietrich Epp

From man 3 setlocale(New maxim: "When in doubt, read the entire manpage."):

来自man 3 setlocale(新格言:“如有疑问,请阅读整个联机帮助页。”):

If locale is "", each part of the locale that should be modified is set according to the environment variables.

如果 locale 是"",则根据环境变量设置应该修改的 locale 的每个部分。

So, we can read the environment variables by calling setlocaleat the beginning of the program, as follows:

所以,我们可以通过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;
}

My system does not support the zh_CNlocale, as the following output reveals:

我的系统不支持zh_CN语言环境,如以下输出所示:

$ ./a.out 
LC_ALL: en_US.utf8
LC_CTYPE: en_US.utf8
$ export LANG=zh_CN.UTF-8
$ ./a.out 
LC_ALL: C
LC_CTYPE: C

Windows:I have no idea about Windows locales. I suggest starting with an MSDN search, and then opening a separateStack Overflow question if you still have questions.

Windows:我不知道 Windows 语言环境。我建议从MSDN 搜索开始,然后如果您仍有问题,则打开一个单独的Stack Overflow 问题。

回答by Deqing

Just figured out how to get locale by C++, simply use an empty string "" to construct std::locale, which does the same thing as setlocale(LC_ALL, "").

刚刚弄清楚如何通过 C++ 获取语言环境,只需使用一个空字符串 "" 来构造 std::locale,它与 setlocale(LC_ALL, "") 的作用相同。

locale l("");
cout<<"Locale by C++: "<<l.name()<<endl;

This linkdescribed differences in details between C locale and C++ locale.

链接描述了 C 语言环境和 C++ 语言环境之间的详细差异。

回答by Riot

A good alternative to consider instead of std::locale is boost::locale which is capable of returning more reliable information - see http://www.boost.org/doc/libs/1_52_0/libs/locale/doc/html/locale_information.html

一个可以考虑代替 std::locale 的好方法是 boost::locale,它能够返回更可靠的信息 - 请参阅http://www.boost.org/doc/libs/1_52_0/libs/locale/doc/html/ locale_information.html

boost::locale::info has the following member functions:

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.

回答by zzz

For Windows use the following code:

对于 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;

This is going to print something like "en-US".

这将打印类似“en-US”的内容。

To purge sublanguage information use the following:

要清除子语言信息,请使用以下命令:

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;

This will give you just "en".

这只会给你“en”。

回答by Synck

The default constructorof std::localecreates a copy of the global C++ locale.

默认构造函数std::locale创建全局C ++语言环境的副本。

So to get the name of the current locale:

所以要获取当前语言环境的名称:

std::cout << std::locale().name() << '\n';