如何让我的环境当前语言环境? [英] How to get current locale of my environment?

查看:103
本文介绍了如何让我的环境当前语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾试图按照Linux的code,但总是在不同的返回'C' LANG 设置。

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

#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

我应该怎么做在Linux下(如Ubuntu),以获得当前区域设置?

What should I do to get current locale setting in Linux(like Ubuntu)?

另一个问题是,是用同样的方式来获得的区域设置在Windows?

Another question is, is it the same way to get locale in Windows?

推荐答案

男子3的setlocale (新格言:如果有疑问,请阅读整个手册页)。

From man 3 setlocale (New maxim: "When in doubt, read the entire manpage."):

如果区域设置为,这应该是根据环境变量设置进行修改语言环境的每个部分。

If locale is "", each part of the locale that should be modified is set according to the environment variables.

因此​​,我们可以通过调用的setlocale 在程序开始读取环境变量,如下:

So, we can read the environment variables by calling setlocale at the beginning of the program, as follows:

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

我的系统不支持 zh_CN的的语言环境,如下面的输出显示:

My system does not support the zh_CN locale, as the following output reveals:


$ ./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系统:我不知道的Windows语言环境的想法。我建议开始与 MSDN搜索,然后打开的单独的的堆栈溢出的问题,如果您还有问题。

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.

这篇关于如何让我的环境当前语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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