Code Monkey home page Code Monkey logo

python-bcp47's Introduction

Build Status codecov

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'

python-bcp47's People

Contributors

comradekingu avatar phlax avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

comradekingu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.