This Python API offers a way to validate and lookup languages tags.

Import the module:

from language_tags import tags

To check whether the language_tag is valid use tags.check(). For example ‘nl-Be’ is valid but ‘nl-BE-BE’ is invalid.

> print(tags.check('nl-BE'))
> print(tags.check('nl-BE-BE'))

For meaningful error output see tags.tag().errors:

> errors = tags.tag('nl-BE-BE').errors
> for err in errors
>    print(err.message)
Extra region subtag 'BE' found.

Lookup descriptions of tags:

> print(tags.description('nl-BE'));
['Dutch', 'Flemish', 'Belgium']

Lookup descriptions of a language subtag:

> print(tags.language('nl').description);
['Dutch', 'Flemish']

Lookup tags by description:

> language_subtags ='Flemish')
> print(language_subtags[0])

Get the language subtag of a tag:

> print(repr(tags.tag('nl-BE').language))
'{"subtag": "nl", "record": {"Subtag": "nl", "Suppress-Script": "Latn", "Added": "2005-10-16", "Type": "language", "Description": ["Dutch", "Flemish"]}, "type": "language"}'

A redundant tag is a grandfathered registration whose individual subtags appear with the same semantic meaning in the registry 1. A redundant tag has descriptions and can have a preferred tag.

> redundant_tag = tags.tag('es-419')
> print(redundant_tag.descriptions)
['Latin American Spanish']
> print(redundant_tag.valid)
> print(redundant_tag.region.description)
['Latin America and the Caribbean']
> print(redundant_tag.region.language)
['Spanish', 'Castilian']

The remainder of the previously registered tags are “grandfathered” 1. Grandfathered tags cannot be parsed into subtags. A grandfathered tag has descriptions. Most grandfathered tags have valid perferred tags.

> grandfathered_tag = tags.tag('i-klingon')
> print(grandfathered_tag.descriptions)
> print(grandfathered_tag.valid)
> print(grandfathered_tag.subtags)
> print(grandfathered_tag.preferred)
> preferred_tag = grandfathered_tag.preferred
> print(preferred_tag.language.description)
['Klingon', 'tlhIngan-Hol']

For the complete api documentation see next chapter.


RFC 5646