-3

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • Possible duplicate of https://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash – wallenborn Oct 22 '19 at 12:55
  • @wallenborn I know how to create a generic dictionary. My question is about how to easily fill these specific dictionaries – Riccardo Bucco Oct 22 '19 at 12:57
  • Welcome to Stack Overflow. SO is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. – Cyrus Oct 22 '19 at 13:14
  • @Cyrus I already implemented my own dictionary filled with those info by simply downloading a file from the Internet and then parsing it line by line. However, I am not able to find a way to do the same without relying on external sources. – Riccardo Bucco Oct 22 '19 at 13:39
  • @Cyrus I'm using Ubuntu 19.10 – Riccardo Bucco Oct 22 '19 at 14:10
  • Related: https://stackoverflow.com/q/4894232/3266847 – Benjamin W. Oct 22 '19 at 14:30
  • 1
    `it contains info about the locales` - locale is not only about language. There is no ISO 693 -like-ish field in [locale](https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html). You _have to_ download the list of codes and parse them, which is exactly the way to go. Also, an environment may not support all locales, but only those it needs to have. I wonder if all languages mentioned in ISO 639 have some locale. – KamilCuk Oct 22 '19 at 16:21

1 Answers1

2

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
dash-o
  • 13,723
  • 1
  • 10
  • 37