Converter Bases

class babelfish.converters.LanguageConverter

A LanguageConverter supports converting an alpha3 language code with an alpha2 country code and a script code into a custom code

codes

Set of possible custom codes

convert(alpha3, country=None, script=None)

Convert an alpha3 language code with an alpha2 country code and a script code into a custom code

Parameters:
  • alpha3 (string) – ISO-639-3 language code
  • country (string or None) – ISO-3166 country code, if any
  • script (string or None) – ISO-15924 script code, if any
Returns:

the corresponding custom code

Return type:

string

Raise:

LanguageConvertError

class babelfish.converters.LanguageReverseConverter

A LanguageConverter able to reverse a custom code into a alpha3 ISO-639-3 language code, alpha2 ISO-3166-1 country code and ISO-15924 script code

reverse(code)

Reverse a custom code into alpha3, country and script code

Parameters:code (string) – custom code to reverse
Returns:the corresponding alpha3 ISO-639-3 language code, alpha2 ISO-3166-1 country code and ISO-15924 script code
Return type:tuple
Raise:LanguageReverseError
class babelfish.converters.LanguageEquivalenceConverter

A LanguageEquivalenceConverter is a utility class that allows you to easily define a LanguageReverseConverter by only specifying the dict from alpha3 to their corresponding symbols.

You must specify the dict of equivalence as a class variable named SYMBOLS.

If you also set the class variable CASE_SENSITIVE to True then the reverse conversion function will be case-sensitive (it is case-insensitive by default).

Example:

class MyCodeConverter(babelfish.LanguageEquivalenceConverter):
    CASE_SENSITIVE = True
    SYMBOLS = {'fra': 'mycode1', 'eng': 'mycode2'}
class babelfish.converters.CountryConverter

A CountryConverter supports converting an alpha2 country code into a custom code

codes

Set of possible custom codes

convert(alpha2)

Convert an alpha2 country code into a custom code

Parameters:alpha2 (string) – ISO-3166-1 language code
Returns:the corresponding custom code
Return type:string
Raise:CountryConvertError
class babelfish.converters.CountryReverseConverter

A CountryConverter able to reverse a custom code into a alpha2 ISO-3166-1 country code

reverse(code)

Reverse a custom code into alpha2 code

Parameters:code (string) – custom code to reverse
Returns:the corresponding alpha2 ISO-3166-1 country code
Return type:string
Raise:CountryReverseError
class babelfish.converters.ConverterManager

Manager for babelfish converters behaving like a dict with lazy loading

Loading is done in this order:

  • Entry point converters
  • Registered converters
  • Internal converters
entry_point

The entry point where to look for converters

internal_converters

Internal converters with entry point syntax

registered_converters = None

Registered converters with entry point syntax

converters = None

Loaded converters

register(entry_point)

Register a converter

Parameters:entry_point (string) – converter to register (entry point syntax)
Raise:ValueError if already registered
unregister(entry_point)

Unregister a converter

Parameters:entry_point (string) – converter to unregister (entry point syntax)

BabelFish

BabelFish is a Python library to work with countries and languages.

PyPI downloads

Donate

If you like babelfish, please consider making a donation

Related Topics

This Page

Fork me on GitHub