Hakan Gundogan
Hakan Gundogan

Reputation: 277

How to convert a language code to language name

Are there any function for converting a language code to language name? For example;

'en' to English,
'ru' to Russian, 'tr' to Turkish

If you help me i will be glad.

Upvotes: 3

Views: 2378

Answers (1)

axtck
axtck

Reputation: 3965

You can accomplish this using Intl:

const getLanguage = (code) => {
    const lang = new Intl.DisplayNames(['en'], {type: 'language'});
    return lang.of(code);
}

const russianLang = getLanguage("ru"); // Russian
console.log(russianLang);

You could write a function that receives a code and then use Intl.DisplayNames to get the according language.

Read more about Intl here.

You could rewrite the function in 1 line if you like:

const getL = (c) => new Intl.DisplayNames(['en'], {type: 'language'}).of(c);
console.log(getL("ru"));

Upvotes: 7

Related Questions