Parser and validator for language codes (bcp47). Also exposes the IANA language code database
Project description
python-bcp47
A validating parser for bcp47 language codes
Data taken from IANA http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
bcp47 spec https://tools.ietf.org/html/bcp47
useful reading: https://www.w3.org/International/articles/language-tags/index.en
extension
and private-use
tag types are not currently supported
Python example
You can read the languages
, extlangs
, scripts
, variants
, regions
, grandfathereds
, and redundants
language tag parts from dicts
on the bcp47
object.
They return OrderedDicts
containing the IANA database information
>>> from bcp47 import bcp47
>>> list(bcp47["languages"].items())[:2]
[('aa', {'Subtag': 'aa', 'Description': ['Afar'], 'Added': '2005-10-16'}), ('ab', {'Subtag': 'ab', 'Description': ['Abkhazian'], 'Added': '2005-10-16', 'Suppress-Script': 'Cyrl'})]
>>> list(bcp47["regions"].items())[:2]
[('AA', {'Subtag': 'AA', 'Description': ['Private use'], 'Added': '2005-10-16'}), ('AC', {'Subtag': 'AC', 'Description': ['Ascension Island'], 'Added': '2009-07-29'})]
`
You can create a language code tag as follows
>>> tag = bcp47(language="en", region="GB")
>>> tag
<bcp47.code.BCP47Code 'en-GB' />
>>> tag.language
'en'
>>> tag.region
'GB'
>>> str(tag)
'en-GB'
You can also pass a string
or list
of args
to create a tag
>>> bcp47("en-GB")
<bcp47.code.BCP47Code 'en-GB' />
>>> bcp47("en", "GB")
<bcp47.code.BCP47Code 'en-GB' />
Creating a tag with invalid or unrecognized parameters raises an BCP47Exception
>>> tag = bcp47(language="NOTALANGUAGE", region="GB")
Traceback (most recent call last):
...
BCP47Exception: Language 'NOTALANGUAGE' not recognized
>>> tag = bcp47("en-NOTAREGION")
Traceback (most recent call last):
...
BCP47Exception: Unrecognized tag part 'NOTAREGION'
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size python_bcp47-0.0.4-py3-none-any.whl (107.1 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size python-bcp47-0.0.4.tar.gz (103.0 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for python_bcp47-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c381759702aacd0adf352b004786924028ab8b071a8aeb13756787ce3a8ed477 |
|
MD5 | e2e32843ef41d89c154a7c2bd7707e59 |
|
BLAKE2-256 | 6a024635f9e0570868f7f1452c5a97c7cc3328b1cea3d6baac6436c0c5212f60 |