Registry / serialization / language-tags

language-tags

JSON →
library1.3.1pypypi✓ verified 23d ago

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.

pip install language-tags
INSTALL
IMPORT
SIG · LANGUAGE-TAGS
L
language-tags
serializationpythonv1.3.1
Install
1.5s avg
Import
89ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.092s · 19.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.086s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Tag
from language_tags import Tag
The primary class for parsing and validating language tags from the `language_tags` module.

Initializes a Tag object from a BCP 47 string, checks its validity, and demonstrates how to access its primary language, region, script, and human-readable description components.

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()}")
Debug
Known issues
breakingPython 2 support was dropped in version 1.0.0. For Python 2 compatibility, you must use version 0.5.0 or earlier.
fix
Upgrade your project to Python 3 or pin the `language-tags` library version to 0.5.0 or earlier.
affects: >=1.0.0
breakingSupport 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.
fix
Ensure your Python environment is version 3.10, 3.11, or 3.12. Update your Python installation if necessary.
affects: >=1.2.0
gotchaOlder 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.
fix
Always use the latest stable version of `language-tags` to ensure you are working with the most current IANA language subtag registry data.
affects: <1.2.0 (relative to current registry)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'language_tags'
This error occurs when the 'language-tags' package is not installed in your Python environment or there's a typo in the import statement. Python cannot find the module to import.
fix
Ensure the package is installed using pip:
`pip install language-tags`

And that your import statement is correct:
`import language_tags`
AttributeError: module 'language_tags' has no attribute 'tags'
This error typically arises when attempting to access a submodule or function named `tags` directly under the `language_tags` module, but the correct way to interact with tags is often through methods like `language_tags.tags.tag()` or `language_tags.tags.check()`, or by importing specific components. The module structure might be different than expected, or you might be trying to access a non-existent attribute.
fix
The 'language-tags' library uses `language_tags.tags.tag()` to create a tag object. If you want to check or validate a tag, you might use methods on the tag object or directly use `language_tags.tags.check()`:

```python
import language_tags.tags

# To create a tag object and then check its validity
tag = language_tags.tags.tag('en-US')
print(tag.valid) # True

# Or to directly check a tag string
is_valid = language_tags.tags.check('en-US')
print(is_valid) # True

is_valid_bad = language_tags.tags.check('invalid-tag')
print(is_valid_bad) # False
```
ValueError: Invalid language tag format
This error occurs when the string passed to a `language-tags` function (like `language_tags.tags.tag()` or `language_tags.tags.check()`) does not conform to the basic structural rules of BCP 47 language tags, even before validation against the IANA registry. This might be due to incorrect subtag ordering, forbidden characters, or other syntactic issues.
fix
Ensure the language tag string follows the basic BCP 47 format. For example, subtags should be separated by hyphens, and certain characters or structures are not allowed. Always refer to BCP 47 guidelines. The library's `check` function can help identify if a tag is invalid early on.

```python
import language_tags.tags

# Correct format
valid_tag = 'en-Latn-US'
print(language_tags.tags.check(valid_tag))

# Incorrect format (e.g., numbers in place of script or region where not allowed)
invalid_tag = 'en-123-US'
print(language_tags.tags.check(invalid_tag))
# If 'strict' mode is enabled or deeper parsing occurs, this could raise ValueError.
# The library's check function usually returns False for such cases without raising an explicit ValueError for format issues.

# Example of a ValueError if you tried to force a non-string or malformed input into a specific function that expects strict parsing:
# try:
#     language_tags.tags.tag(['en', 'US']) # Will likely raise a TypeError or similar before ValueError
# except ValueError as e:
#     print(e)
```
TypeError: argument of type 'Tag' is not iterable
This error typically occurs when you treat a `Tag` object returned by `language_tags.tags.tag()` (or similar functions) as if it were a list or another iterable, attempting to iterate over it directly. The `Tag` object itself is not designed for direct iteration to access its components; you should use its specific attributes (e.g., `.language`, `.script`, `.region`) or methods.
fix
Access the components of a `Tag` object using its attributes, not by iterating over the object itself.

```python
import language_tags.tags

tag_obj = language_tags.tags.tag('en-GB')

# Incorrect (will raise TypeError):
# for component in tag_obj:
#     print(component)

# Correct way to access components:
print(f"Language: {tag_obj.language}")
print(f"Script: {tag_obj.script}") # Will be None if not explicitly present
print(f"Region: {tag_obj.region}")

# To get all subtags as a list, you might need to combine them manually or look for a specific method.
# The 'subtags' attribute is available:
print(f"All subtags: {tag_obj.subtags}")
```
Upgrade
Version history
1.3.1latest on PyPI · released May 8, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
language-tags — pip install language-tags · libregistry