The standard is pretty much silent on what constitutes a valid locale name; only that passing an invalid locale name results in std::runtime_error
. What locale names are usable on common windows compilers such as MSVC, MinGW, and ICC?
对于什么构成有效的语言环境名,标准几乎保持沉默;只有传递无效的语言环境名称才会导致std::runtime_error。在MSVC、MinGW和ICC等常用的windows编译器上,哪些语言环境名称是可用的?
3 个解决方案
#1
19
I believe the information you need is here :
我相信你需要的信息在这里:
locale "lang[_country_region[.code_page]]"
| ".code_page"
| ""
| NULL
This page provides links to :
本页提供连结至:
- Language Strings
- 语言字符串
- Country/Region String
- 国家/地区的字符串
- Code Pages
- 代码页
Although my answers covers setlocale
instead of std::locale
, this MSDN page seems to imply that the format is indeed the same :
虽然我的回答涵盖了setlocale而不是std::locale,但这个MSDN页面似乎暗示了格式确实是相同的:
An object of class locale also stores a locale name as an object of class string. Using an invalid locale name to construct a locale facet or a locale object throws an object of class
runtime_error
. The stored locale name is "*" if the locale object cannot be certain that a C-style locale corresponds exactly to that represented by the object. Otherwise, you can establish a matching locale within the Standard C Library, for the locale object loc, by callingsetlocale(LC_ALL, loc.name.c_str)
.类locale的对象还将locale名称存储为类string的对象。使用一个无效的区域名称来构造一个locale facet或一个locale对象,就会抛出一个类runtime_error的对象。如果语言环境对象不能确定c风格的语言环境与该对象表示的语言环境完全对应,则存储的语言环境名称为“*”。否则,您可以通过调用setlocale(LC_ALL, location .name.c_str)在标准C库中为locale对象loc建立一个匹配的locale。
Also see this page and this thread which tend to show that std::locale
internally uses setlocale
.
还可以看到这个页面和这个线程,它倾向于显示std::locale在内部使用setlocale。
#2
23
Ok, there is a difference between C and C++ locales.
好的,C和c++的locale有区别。
Let's start:
让我们开始:
-
MSVC C++ std::locale and C setlocale
MSVC c++ std::locale和C setlocale
Accepts locale names as "Language[_Country][.Codepage]" for example "English_United States.1251" Otherwise would throws. Note: codepage can't be 65001/UTF-8 and should be consistent with ANSI codepage for this locale (or just omitted)
接受语言环境名称为“语言[_Country][。例如,“English_United States.1251”否则将抛出。注意:代码页不能是65001/UTF-8,并且应该与此语言环境的ANSI代码页一致(或略)
-
MSVC C++ std::locale and C setlocale in Vista and 7 should accept locales [Language][-Script][-Country] like "en-US" using ISO-631 language codes and ISO 3166 regions and script names.MSVC c++ std::在Vista中,locale和C setlocale和7应该接受locale [Language][-Script][-Country],比如"en-US",使用ISO-631语言代码和ISO 3166区域和脚本名。
I tested it with Visual Studio on Windows 7 - it does not work.
我在Windows 7上用Visual Studio测试了它——它不工作。
-
MinGW C++ std::locale accepts "C" and "POSIX" it does not support other locales, actually gcc supports locales only over GNU C library - basically only under Linux.
语言环境接受“C”和“POSIX”,它不支持其他语言环境,实际上gcc只支持GNU C库中的语言环境——基本上只支持Linux下的语言环境。
setlocale is native Windows API call so should support all I mentioned above.
setlocale是本机Windows API调用,因此应该支持我上面提到的所有内容。
It may support wider range of locales when used with alternative C++ libraries like Apache stdcxx or STL Port.
当与其他c++库(如Apache stdcxx或STL端口)一起使用时,它可能支持更广泛的地区范围。
-
ICC - I hadn't tested it but it depends on the standard C++ library it uses. For example under Linux it used GCC's libstdc++ so it supports all the locales gcc supports. I don't know what standard C++ library it uses under Windows.
我没有测试过它,但是它取决于标准的c++库。例如,在Linux下,它使用了GCC的libstdc+,因此它支持GCC支持的所有地区。我不知道在Windows下使用什么标准c++库。
If you want to have "compiler and platform" independent locales support (and actually much better support) take a look on Boost.Locale
如果您想拥有“编译器和平台”独立的地区环境支持(实际上更好的支持),请查看boot . locale
Artyom
Artyom
#3
10
Here's one locale name that's usable pretty much anywhere: ""
. That is, the empty string. The is in contrast to the "C"
locale that you are probably getting by default. The empty string as an argument to std::setlocale()
means something like "Use the preferred locale set by the user or environment." If you use this, the downside is that your program won't have the same output everywhere; the upside is that your users might think it works just the way they want.
这里有一个几乎可以在任何地方使用的语言环境名:""。即空字符串。与您可能在默认情况下得到的“C”语言环境相反。空字符串作为std::setlocale()的参数,表示“使用用户或环境设置的首选语言环境”。如果你使用这个,缺点是你的程序不会有相同的输出;好处是,你的用户可能会认为它按照他们想要的方式工作。
#1
19
I believe the information you need is here :
我相信你需要的信息在这里:
locale "lang[_country_region[.code_page]]"
| ".code_page"
| ""
| NULL
This page provides links to :
本页提供连结至:
- Language Strings
- 语言字符串
- Country/Region String
- 国家/地区的字符串
- Code Pages
- 代码页
Although my answers covers setlocale
instead of std::locale
, this MSDN page seems to imply that the format is indeed the same :
虽然我的回答涵盖了setlocale而不是std::locale,但这个MSDN页面似乎暗示了格式确实是相同的:
An object of class locale also stores a locale name as an object of class string. Using an invalid locale name to construct a locale facet or a locale object throws an object of class
runtime_error
. The stored locale name is "*" if the locale object cannot be certain that a C-style locale corresponds exactly to that represented by the object. Otherwise, you can establish a matching locale within the Standard C Library, for the locale object loc, by callingsetlocale(LC_ALL, loc.name.c_str)
.类locale的对象还将locale名称存储为类string的对象。使用一个无效的区域名称来构造一个locale facet或一个locale对象,就会抛出一个类runtime_error的对象。如果语言环境对象不能确定c风格的语言环境与该对象表示的语言环境完全对应,则存储的语言环境名称为“*”。否则,您可以通过调用setlocale(LC_ALL, location .name.c_str)在标准C库中为locale对象loc建立一个匹配的locale。
Also see this page and this thread which tend to show that std::locale
internally uses setlocale
.
还可以看到这个页面和这个线程,它倾向于显示std::locale在内部使用setlocale。
#2
23
Ok, there is a difference between C and C++ locales.
好的,C和c++的locale有区别。
Let's start:
让我们开始:
-
MSVC C++ std::locale and C setlocale
MSVC c++ std::locale和C setlocale
Accepts locale names as "Language[_Country][.Codepage]" for example "English_United States.1251" Otherwise would throws. Note: codepage can't be 65001/UTF-8 and should be consistent with ANSI codepage for this locale (or just omitted)
接受语言环境名称为“语言[_Country][。例如,“English_United States.1251”否则将抛出。注意:代码页不能是65001/UTF-8,并且应该与此语言环境的ANSI代码页一致(或略)
-
MSVC C++ std::locale and C setlocale in Vista and 7 should accept locales [Language][-Script][-Country] like "en-US" using ISO-631 language codes and ISO 3166 regions and script names.MSVC c++ std::在Vista中,locale和C setlocale和7应该接受locale [Language][-Script][-Country],比如"en-US",使用ISO-631语言代码和ISO 3166区域和脚本名。
I tested it with Visual Studio on Windows 7 - it does not work.
我在Windows 7上用Visual Studio测试了它——它不工作。
-
MinGW C++ std::locale accepts "C" and "POSIX" it does not support other locales, actually gcc supports locales only over GNU C library - basically only under Linux.
语言环境接受“C”和“POSIX”,它不支持其他语言环境,实际上gcc只支持GNU C库中的语言环境——基本上只支持Linux下的语言环境。
setlocale is native Windows API call so should support all I mentioned above.
setlocale是本机Windows API调用,因此应该支持我上面提到的所有内容。
It may support wider range of locales when used with alternative C++ libraries like Apache stdcxx or STL Port.
当与其他c++库(如Apache stdcxx或STL端口)一起使用时,它可能支持更广泛的地区范围。
-
ICC - I hadn't tested it but it depends on the standard C++ library it uses. For example under Linux it used GCC's libstdc++ so it supports all the locales gcc supports. I don't know what standard C++ library it uses under Windows.
我没有测试过它,但是它取决于标准的c++库。例如,在Linux下,它使用了GCC的libstdc+,因此它支持GCC支持的所有地区。我不知道在Windows下使用什么标准c++库。
If you want to have "compiler and platform" independent locales support (and actually much better support) take a look on Boost.Locale
如果您想拥有“编译器和平台”独立的地区环境支持(实际上更好的支持),请查看boot . locale
Artyom
Artyom
#3
10
Here's one locale name that's usable pretty much anywhere: ""
. That is, the empty string. The is in contrast to the "C"
locale that you are probably getting by default. The empty string as an argument to std::setlocale()
means something like "Use the preferred locale set by the user or environment." If you use this, the downside is that your program won't have the same output everywhere; the upside is that your users might think it works just the way they want.
这里有一个几乎可以在任何地方使用的语言环境名:""。即空字符串。与您可能在默认情况下得到的“C”语言环境相反。空字符串作为std::setlocale()的参数,表示“使用用户或环境设置的首选语言环境”。如果你使用这个,缺点是你的程序不会有相同的输出;好处是,你的用户可能会认为它按照他们想要的方式工作。