如何在Windows上为C ++程序设置正确的初始语言环境? [英] how do I set the proper initial locale for a C++ program on Windows?

查看:46
本文介绍了如何在Windows上为C ++程序设置正确的初始语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对本地化编程还很陌生,我想弄清楚如何为新启动的非托管C ++应用程序(从应用程序内部)设置适当的初始语言环境.

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语言环境开始,而不是适当的区域语言环境(英语,德语等).所以我需要做的是调用 setlocale(LC_ALL,"???"),但是我不确定如何为第二个参数获取正确的值.它将类似于英语"或德语:德语"-基本上是用户通过区域和语言选项"控制面板设置的语言环境.为了清楚起见,我正在寻找如何格式化语言环境字符串,我正在为运行应用程序的计算机寻找正确的语言环境字符串.

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 not looking for how to format the locale string, I'm looking for the correct locale string for the computer where the app is running.

我猜想有些Win32 API可以给我这个,或者注册表项可能包含正确的值.有人知道我应该做什么吗?

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?

推荐答案

setlocale()是C,而不是C ++.我隐约记得在VC6上看到两者之间的干扰,但这是一个错误.通常,setlocale()仅影响C函数的行为.

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.

在C ++中,本地化由std :: locale类控制.默认情况下,对语言环境敏感的操作使用全局语言环境,该语言环境是通过默认构造语言环境对象而获得的,并且可以使用std :: locale :: global(const std :: locale&)进行设置.

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&).

使用空字符串(std :: locale("))构造一个语言环境对象会创建一个与程序环境相对应的语言环境.

Constructing a locale object with an empty string (std::locale("")) creates a locale corresponding to the program's environment.

在程序启动时,全局语言环境是"C"或经典"语言环境.要将全局语言环境设置为程序的环境语言环境(我想这就是您要的内容),您可以这样编写:

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:

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;
}

打印:

C
French_Canada.1252
C

这篇关于如何在Windows上为C ++程序设置正确的初始语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆