Howto get the language name for a given locale in linux

北城余情 提交于 2020-06-27 08:46:40

问题


This is pretty much This question with 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.

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.

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.


回答1:


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

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



回答2:


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


来源:https://stackoverflow.com/questions/4894232/howto-get-the-language-name-for-a-given-locale-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!