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-tagsVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your project to Python 3 or pin the `language-tags` library version to 0.5.0 or earlier.
Ensure your Python environment is version 3.10, 3.11, or 3.12. Update your Python installation if necessary.
Always use the latest stable version of `language-tags` to ensure you are working with the most current IANA language subtag registry data.
Ensure the package is installed using pip: `pip install language-tags` And that your import statement is correct: `import language_tags`
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
```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) ```
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}")
```No dependency data recorded yet.