Skip to main content

A Python package to validate BCP47 language tags

Project description

IANA-BCP47 (BCP47 Validator)

This project provides a Python implementation of a BCP47 language tag validator. It validates language tags based on the IANA Language Subtag Registry and ensures compliance with BCP47 specifications. The validator supports checking for redundant tags and validating custom language codes using dictionaries of subtags.

Features

  • Validates BCP47 language tags with dictionaries derived from the IANA Language Subtag Registry.
  • Supports validation for the following subtag types:
    • language
    • extlang
    • script
    • region
    • variant
    • redundant
  • Provides detailed descriptions of valid language tags.
  • Returns None for invalid tags.

How It Works

The project uses a generated Python module (bcp47.py) containing dictionaries for each type of subtag. Each language tag is validated as follows:

  1. The first subtag must match a valid language code.
  2. Subsequent subtags are validated against dictionaries for extlang, script, region, and variant.
  3. Redundant tags (full language tags) are checked separately.
  4. Return a tuple with two elements:
    • The first element is a boolean indicating whether the tag is valid.
    • The second element is a string describing the language tag or error message if the tag is invalid.

Usage

[!IMPORTANT]

The IANA-BCP47 library validates whether a given string conforms to the BCP 47 standard format. However, it does not verify whether the string represents a practical or commonly used language code. For instance, ru-US is a valid BCP 47 format but is not a language code typically used in real-world applications.

Validate a BCP47 Language Tag

Use the validate_bcp47 function to validate a language tag and retrieve its description.

pip install iana-bcp47
from iana_bcp47.validator import validate_bcp47

# Example usage
tags = ["en", "en-US", "zh-Hant", "zh-Hant-CN", "invalid-tag", 'zh-US']

for tag in tags:
    valid, msg = validate_bcp47(tag)
    print(f"Tag '{tag}' is {'valid: ' + msg if valid else 'invalid'}.")

Output Example

Tag 'en' is valid: English.
Tag 'en-US' is valid: English - United States.
Tag 'zh-Hant' is valid: traditional Chinese.
Tag 'zh-Hant-CN' is valid: PRC Mainland Chinese in traditional script.
Tag 'invalid-tag' is invalid.
Tag 'zh-US' is valid: Chinese - United States.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iana_bcp47-0.1.2.tar.gz (92.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iana_bcp47-0.1.2-py3-none-any.whl (92.9 kB view details)

Uploaded Python 3

File details

Details for the file iana_bcp47-0.1.2.tar.gz.

File metadata

  • Download URL: iana_bcp47-0.1.2.tar.gz
  • Upload date:
  • Size: 92.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for iana_bcp47-0.1.2.tar.gz
Algorithm Hash digest
SHA256 07e6e26a070c968c765f8448ef8824f5f3a8f6169f7157f75f4222e44ae91e2a
MD5 27d0d0b97bcb71656989fdb28f27a615
BLAKE2b-256 b382ca628e4f5b31b23d7ed5c39c5d7c9389fee61604c368d79482f3b096ef78

See more details on using hashes here.

Provenance

The following attestation bundles were made for iana_bcp47-0.1.2.tar.gz:

Publisher: publish.yml on Masterain98/IANA-BCP47

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file iana_bcp47-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: iana_bcp47-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 92.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for iana_bcp47-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d3b5d8f842bcf0014f6867014dca9e856226e4caa2bad88d8199be335dcd0606
MD5 22840b20efdf91871e7376a0516c6691
BLAKE2b-256 b46728b5930c5be20300aa9850d94e27a35b6a8c36f858fb21a9fe5e5c241ac0

See more details on using hashes here.

Provenance

The following attestation bundles were made for iana_bcp47-0.1.2-py3-none-any.whl:

Publisher: publish.yml on Masterain98/IANA-BCP47

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page