如何获得我的环境的当前语言环境?

时间:2022-08-12 07:22:03

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

曾尝试在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中获取语言环境也是同样的方法吗?

3 个解决方案

#1


19  

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.

如果语言环境是“”,那么应该修改的语言环境的每个部分都将根据环境变量进行设置。

So, we can read the environment variables by calling setlocale at 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_CN locale, 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 separate Stack Overflow question if you still have questions.

Windows:我对Windows环境一无所知。我建议从MSDN搜索开始,如果您还有问题,请打开一个单独的堆栈溢出问题。

#2


18  

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++获取locale,只需使用一个空字符串""来构造std:::locale,它与setlocale(LC_ALL, ")做同样的事情。

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

This link described differences in details between C locale and C++ locale.

这个链接描述了C语言环境和c++语言环境之间的差异。

#3


2  

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_inform.html

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

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.

#1


19  

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.

如果语言环境是“”,那么应该修改的语言环境的每个部分都将根据环境变量进行设置。

So, we can read the environment variables by calling setlocale at 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_CN locale, 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 separate Stack Overflow question if you still have questions.

Windows:我对Windows环境一无所知。我建议从MSDN搜索开始,如果您还有问题,请打开一个单独的堆栈溢出问题。

#2


18  

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++获取locale,只需使用一个空字符串""来构造std:::locale,它与setlocale(LC_ALL, ")做同样的事情。

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

This link described differences in details between C locale and C++ locale.

这个链接描述了C语言环境和c++语言环境之间的差异。

#3


2  

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_inform.html

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

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.