如何在 Windows 上为 C++ 程序设置正确的初始语言环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/571359/
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
how do I set the proper initial locale for a C++ program on Windows?
提问by Charlie
I'm fairly new to localized programming, and I'm trying to figure out how to set the proper initial locale for a newly-launched unmanaged C++ application (from within the app).
我对本地化编程还很陌生,我正在尝试弄清楚如何为新启动的非托管 C++ 应用程序(从应用程序内部)设置正确的初始语言环境。
As far as I can tell, new applications start with the C locale, rather than the proper regional locale (English, German, etc). So what I need to do is call setlocale( LC_ALL, "???" )
, but I'm not sure how to get the correct value for the second argument. It will be something like "English" or "German:Germany" - basically whatever locale was set by the user via the Regional and Language Options control panel. Just to be clear, I'm notlooking for how to format the locale string, I'm looking for the correct locale string for the computer where the app is running.
据我所知,新应用程序以 C 语言环境开始,而不是正确的区域语言环境(英语、德语等)。所以我需要做的是 call setlocale( LC_ALL, "???" )
,但我不确定如何获得第二个参数的正确值。它将类似于“English”或“German:Germany”——基本上是用户通过区域和语言选项控制面板设置的任何区域设置。需要说明的是,我不是在寻找如何格式化语言环境字符串,而是在为运行应用程序的计算机寻找正确的语言环境字符串。
I'm guessing that there's some Win32 API that would give me this, or perhaps a registry key that would contain the proper value. Does anybody know what I should be doing?
我猜有一些 Win32 API 会给我这个,或者可能是一个包含正确值的注册表项。有人知道我应该做什么吗?
回答by éric Malenfant
setlocale() is C, not C++. I vaguely remember seeing interference between the two on VC6, but that was a bug. Normally, setlocale() affects the behavior of the C functions only.
setlocale() 是 C,而不是 C++。我依稀记得在 VC6 上看到过两者之间的干扰,但那是一个错误。通常, setlocale() 仅影响 C 函数的行为。
In C++, localization is controlled by the std::locale class. By default, locale-sensitive operations use the global locale, which is obtained by default-constructing a locale object, and can be set with std::locale::global(const std::locale&).
在 C++ 中,本地化由 std::locale 类控制。默认情况下,locale-sensitive 操作使用全局 locale,它是通过默认构造 locale 对象获得的,并且可以用 std::locale::global(const std::locale&) 设置。
Constructing a locale object with an empty string (std::locale("")) creates a locale corresponding to the program's environment.
使用空字符串 (std::locale("")) 构造区域设置对象会创建与程序环境对应的区域设置。
At program startup, the global locale is the "C" or "Classic" locale. To set the global locale to the program's environment locale (which I guess is what you're asking), you thus write:
在程序启动时,全局语言环境是“C”或“经典”语言环境。要将全局语言环境设置为程序的环境语言环境(我猜这就是您要问的),您可以这样写:
std::locale::global(std::locale(""));
For example, my regional settings are currently set to French(Canada). Running this:
例如,我的区域设置当前设置为法语(加拿大)。运行这个:
int main(void)
{
std::cout << std::locale().name() << std::endl;
std::locale::global(std::locale(""));
std::cout << std::locale().name() << std::endl;
std::locale::global(std::locale("C"));
std::cout << std::locale().name() << std::endl;
return 0;
}
prints:
印刷:
C
French_Canada.1252
C