Get the name of a language given its code, and vice versa

359 Views Asked by At

How can I get the English name of a language given its two-letter code (see ISO 639-1 and List of ISO 639-1 codes)?

en --> english
it --> italian

And what about the other way around?

spanish --> es
finnish --> fi

Of course, a possible way to do it is to download a file containing those info from the Internet (for example the file that can be found here) and then read it line by line, extracting the info you need. However, I was wondering whether it is possible to rely only on the OS (Ubuntu, in my case): it contains info about the locales, so I guess there exists a way to do it.

1

There are 1 best solutions below

0
On

If you have Perl installed, you can use the core module 'Locale::Langauge' to convert 2 letter languages to name, and in reverse

perl -e 'use Locale::Language; print code2language("ar");'
Arabic

perl -MLocale::Language -e 'print language2code("French");'
fr

See https://perldoc.perl.org/5.8.8/Locale/Language.html