如何在 linux 中获取给定语言环境的语言名称

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

Howto get the language name for a given locale in linux

linuxlocale

提问by Dushara

This is pretty much This questionwith a bit more information. My goal is to work out the languages installed in the system.

这几乎是这个问题,有更多信息。我的目标是找出系统中安装的语言。

The following command

以下命令

locale -a 

displays all the languages (in a format such as en_AU.utf8). This seems to correspond to the contents of /usr/lib/locale.

显示所有语言(以 en_AU.utf8 等格式)。这似乎对应于 /usr/lib/locale 的内容。

Furthermore, invoking

此外,调用

LANG=fr_FR.utf8 locale -ck LC_IDENTIFICATION

Gives information of that particular locale which includes the language name (Which in this case is French).

提供该特定语言环境的信息,其中包括语言名称(在本例中为法语)。

This seems to be the information contained in /usr/lib/locale/fr_FR.utf8/LC_IDENTIFICATION.

这似乎是/usr/lib/locale/fr_FR.utf8/LC_IDENTIFICATION 中包含的信息。

Is there a way (maybe an API call) to obtain this info? I looked at the source of the locale utility but it uses a private struct.

有没有办法(也许是 API 调用)来获取这些信息?我查看了 locale 实用程序的源代码,但它使用了私有结构。

采纳答案by Dushara

Thanks to Yasir. This is exactly what I wanted:

感谢亚西尔。这正是我想要的:

#include <langinfo.h>

char *s;
s = getenv("LANG");
if (s == NULL) 
    printf("LANG is not set");
else {
    setlocale(LC_ALL, s);
    printf(nl_langinfo(_NL_IDENTIFICATION_LANGUAGE));
}

回答by Yasir Arsanukaev

I think, you could just get environment variables, using, for example, getenv(3), thus you would want to pass it the name of variable, e. g.:

我认为,您可以使用例如,获取环境变量,getenv(3)因此您希望将变量的名称传递给它,例如:

char *s;
s = getenv("LANG");
if (s == NULL) 
    printf("LANG is not set");
else
    printf(s);