Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of language codes (ISO639-1) in Python?

Tags:

python

Is it possible to get the list of all ISO639-1 language codes from pycountry 1.15? For example, ['en','it','el','fr',...]? If yes then how?

The following doesn't work I am afraid:

import pycountry
pycountry.languages
like image 580
user706838 Avatar asked Sep 25 '15 00:09

user706838


People also ask

What are ISO language codes?

ISO 639 is a standardized nomenclature used to classify languages. Each language is assigned a two-letter (639-1) and three-letter (639-2 and 639-3) lowercase abbreviation, amended in later versions of the nomenclature.

How many languages are used in ISO?

There are 58 languages in ISO 639-2 which are considered, for the purposes of the standard, to be "macrolanguages" in ISO 639-3.


1 Answers

This will give you a list of two-digit ISO3166-1 country codes:

countries = [country.alpha2 for country in pycountry.countries]
#countries = [country.alpha_2 for country in pycountry.countries]  # for python3 

This will give you a list of two-digit ISO639-1 language codes:

langs = [lang.iso639_1_code
         for lang in pycountry.languages
         if hasattr(lang, 'iso639_1_code')]

This will give you a list of all of the language codes:

langs = [getattr(lang, 'iso639_1_code', None) or 
         getattr(lang, 'iso639_2T_code', None) or 
         getattr(lang, 'iso639_3_code') 
         for lang in pycountry.languages]
like image 133
Robᵩ Avatar answered Oct 12 '22 19:10

Robᵩ