常见的 Windows 编译器上有哪些 std::locale 名称可用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4406895/
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-09-15 15:46:35  来源:igfitidea点击:

What std::locale names are available on common windows compilers?

c++windowslocale

提问by Billy ONeal

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. 哪些语言环境名称可用于常见的 Windows 编译器,例如 MSVC、MinGW 和 ICC?

采纳答案by icecrime

I believe the information you need is here:

我相信您需要的信息在这里

locale  "lang[_country_region[.code_page]]"
            | ".code_page"
            | ""
            | NULL

This page provides links to :

此页面提供以下链接:



Although my answers covers setlocaleinstead of std::locale, this MSDN pageseems 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 calling setlocale(LC_ALL, loc.name.c_str).

类语言环境的对象还将语言环境名称存储为类字符串的对象。使用无效的语言环境名称来构造语言环境方面或语言环境对象会抛出 class 的对象runtime_error。如果语言环境对象不能确定 C 风格的语言环境与该对象所表示的语言环境完全对应,则存储的语言环境名称为“*”。 否则,您可以通过调用在标准 C 库中为语言环境对象 loc 建立匹配的语言环境setlocale(LC_ALL, loc.name.c_str)

Also see this pageand this threadwhich tend to show that std::localeinternally uses setlocale.

另请参阅此页面此线程,它们倾向于表明std::locale内部使用setlocale.

回答by Artyom

Ok, there is a difference between C and C++ locales.

好的,C 和 C++ 语言环境之间存在差异。

Let's start:

开始吧:

  • MSVC C++ std::locale and 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)

  • 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.

    I tested it with Visual Studio on Windows 7 - it does not work.

  • 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.

    setlocale is native Windows API call so should support all I mentioned above.

    It may support wider range of locales when used with alternative C++ libraries like Apache stdcxx or STL Port.

  • 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.

  • MSVC C++ std::locale 和 C setlocale

    接受语言环境名称为“Language[_Country][.Codepage]”,例如“English_United States.1251”,否则会抛出异常。注意:代码页不能是 65001/UTF-8 并且应该与此语言环境的 ANSI 代码页一致(或只是省略)

  • Vista 和 7 中的 MSVC C++ std::locale 和 C setlocale 应该接受区域设置 [Language][-Script][-Country],例如使用 ISO-631 语言代码和 ISO 3166 区域和脚本名称的“en-US”。

    我在 Windows 7 上使用 Visual Studio 对其进行了测试 - 它不起作用。

  • MinGW C++ std::locale 接受“C”和“POSIX”,它不支持其他语言环境,实际上 gcc 仅通过 GNU C 库支持语言环境——基本上只在 Linux 下。

    setlocale 是本机 Windows API 调用,因此应该支持我上面提到的所有内容。

    当与 Apache stdcxx 或 STL Port 等替代 C++ 库一起使用时,它可能支持更广泛的语言环境。

  • ICC - 我没有测试过它,但这取决于它使用的标准 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

如果您想拥有“编译器和平台”独立的语言环境支持(实际上更好的支持),请查看 Boost.Locale

Artyom

阿尔乔姆

回答by John Zwinck

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()意味着类似于“使用用户或环境设置的首选语言环境”。如果你使用它,缺点是你的程序不会在任何地方都有相同的输出;好处是您的用户可能认为它按他们想要的方式工作。