Package: iso639-lang
iso639-langiso639-langis a simple library to handle the ISO 639 series of international standards for language codes.>>>fromiso639importLang>>>Lang("fr")Lang(name='French',pt1='fr',pt2b='fre',pt2t='fra',pt3='fra',pt5='')iso639-lang allows you to switch from one language code to another easily.
There’s no need to manually download or parse data files, just use theLangclass!ISO 639-1, ISO 639-2, ISO 639-3 and ISO 639-5 parts are all supported.Installing iso639-lang and Supported Versionsiso639-lang is available on PyPI:$pipinstalliso639-langiso639-lang supports Python 3.7+.UsageHandling language codes with iso639-lang is very simple.Begin by importing theLangclass:>>>fromiso639importLangLangis instantiable with any ISO 639 language code or name. For example, let’s try to get the ISO 639 codes for French:>>>lg=Lang("French")>>>lg.name'French'>>>lg.pt1'fr'>>>lg.pt2b'fre'>>>lg.pt2t'fra'>>>lg.pt3'fra'>>>lg.pt5''You can use theasdictmethod to return ISO 639 language values as a Python dictionary.>>>lg.asdict(){'name':'French','pt1':'fr','pt2b':'fre','pt2t':'fra','pt3':'fra','pt5':''}In data structuresLists ofLanginstances are sortable by name.>>>langs=[Lang("deu"),Lang("eng"),Lang("rus"),Lang("eng")]>>>[lg.nameforlginsorted(langs)]['English','English','German','Russian']AsLangis hashable,Langinstances can be added to a set or used as dictionary keys.>>>[lg.pt3forlginset(langs)]['eng','rus','deu']Iteratoriter_langs()iterates through all possibleLanginstances, ordered alphabetically by name.>>>fromiso639importiter_langs>>>[lg.nameforlginiter_langs()]["'Are'are","'Auhelawa","A'ou",...,'ǂHua','ǂUngkue','ǃXóõ']Language TypesThe type of a language is accessible thanks to thetypemethod.>>>lg=Lang("Latin")>>>lg.type()'Ancient'MacrolanguagesYou can easily determine whether a language is a macrolanguage or an individual language.>>>lg=Lang("Arabic")>>>lg.scope()'Macrolanguage'Use themacromethod to get the macrolanguage of an individual language.>>>lg=Lang("Wu Chinese")>>>lg.macro()Lang(name='Chinese',pt1='zh',pt2b='chi',pt2t='zho',pt3='zho',pt5='')Conversely, you can also list all the individual languages that share a common macrolanguage.>>>lg=Lang("Persian")>>>lg.individuals()[Lang(name='Iranian Persian',pt1='',pt2b='',pt2t='',pt3='pes',pt5=''),Lang(name='Dari',pt1='',pt2b='',pt2t='',pt3='prs',pt5='')]ExceptionsWhen an invalid language value is passed toLang, anInvalidLanguageValueexception is raised.>>>fromiso639.exceptionsimportInvalidLanguageValue>>>try:...Lang("foobar")...exceptInvalidLanguageValuease:...e.msg..."'foobar' not supported by ISO 639"When an deprecated language value is passed toLang, aDeprecatedLanguageValueexception is raised.>>>fromiso639.exceptionsimportDeprecatedLanguageValue>>>try:...Lang("gsc")...exceptDeprecatedLanguageValuease:...lg=Lang(e.change_to)...f"{e.name}replaced by{lg.name}"...'Gascon replaced by Occitan (post 1500)'Sources of data used by iso639-langAs of November 12, 2023, iso639-lang is based on the latest official code tables provided by the ISO 639 registration authorities.StandardNameRegistration AuthorityISO 639-1Part 1: Alpha-2 codeInfotermISO 639-2Part 2: Alpha-3 codeLibrary of CongressISO 639-3Part 3: Alpha-3 code for comprehensive coverage of languagesSIL InternationalISO 639-4Part 4: Implementation guidelines and general principles for language coding(not a list)ISO/TC 37/SC 2ISO 639-5Part 5: Alpha-3 code for language families and groupsLibrary of Congress