confucius_007
confucius_007

Reputation: 326

Is there any API in Linux to get the full language name from the locale?

I want to get the complete language name from the locale in Linux. For example, in Windows, there is one API GetLocaleInfoEx we can use, it will return "English" for locale "en-US".

wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
GetLocaleInfoEx(L"en-US", LOCALE_SENGLISHLANGUAGENAME,
            (LPWSTR)buffer, LOCALE_NAME_MAX_LENGTH)

This will fill the buffer with "English". Is there anything similar in Linux?

Upvotes: 2

Views: 744

Answers (2)

confucius_007
confucius_007

Reputation: 326

After playing around with the other answers, I thought of concluding it here.

   #include <langinfo.h>
   #include <locale.h> // for LC_ALL_MASK flag, freelocale(), and newlocale() functions.

Access locale information from the system locale.

   char *nl_langinfo(nl_item item); 

Access locale information from the locale given as a parameter.

   char *nl_langinfo_l(nl_item item, locale_t locale);

Use of GetLocaleInfoEx in the given example equivalent to the following on Linux.

locale_t loc = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL); 
if (loc) {
    language = strdup(nl_langinfo_l(_NL_IDENTIFICATION_LANGUAGE, loc));
    freelocale(loc);
   }

Refer to nl_langinfo for more details.

Upvotes: 1

Andrei Stoicescu
Andrei Stoicescu

Reputation: 729

You can use

nl_langinfo(_NL_IDENTIFICATION_LANGUAGE)

from

#include <langinfo.h>

if locale is not set, you can set it with

s = getenv("LANG");
setlocale(LC_ALL, s);

Upvotes: 1

Related Questions