Language Tags
This project is a Python version of the language-tags Javascript project. It provides an API to validate and lookup language tags based on BCP 47 (RFC 5646) and the latest IANA language subtag registry. It is actively maintained, with updates released as the underlying standards change.
Warnings
- breaking Python 2 support was dropped in version 1.0.0. For Python 2 compatibility, you must use version 0.5.0 or earlier.
- breaking Support for Python versions 3.6 and 3.7 was dropped in version 1.2.0. The library now explicitly requires Python >=3.10, <3.13.
- gotcha Older versions of the library may contain outdated IANA language subtag registry data, which can lead to incorrect validation or lookup results for new or changed language tags. Compliance with BCP 47 depends on up-to-date data.
Install
-
pip install language-tags
Imports
- Tag
from language_tags import Tag
Quickstart
from language_tags import Tag
# Create a Tag object from a BCP 47 string
tag = Tag('en-US')
# Check if the tag is valid according to the IANA registry
print(f"'{tag}' is valid: {tag.is_valid()}")
# Access components of the tag
print(f"Language: {tag.language()}")
print(f"Region: {tag.region()}")
# Script might be None if not explicitly present or suppressed
print(f"Script: {tag.script()}")
print(f"Description: {tag.description()}")
# Example of an invalid tag
invalid_tag = Tag('xx-YYY')
print(f"\n'{invalid_tag}' is valid: {invalid_tag.is_valid()}")