babelfish

raw JSON →
0.6.1 verified Fri May 01 auth: no python

A Python library for working with countries and languages, providing enums, conversion, and lookup utilities. Current version is 0.6.1 (released 2023-11-26), with a slow release cadence (~yearly). Supports Python >=3.8.

pip install babelfish
error ImportError: cannot import name 'Country' from 'babelfish'
cause Partial or old installation, or wrong import path.
fix
Reinstall with: pip install --upgrade babelfish. The correct import is: from babelfish import Country
error AttributeError: module 'babelfish' has no attribute 'Country'
cause File named babelfish.py in current directory shadows the library.
fix
Rename your local file (e.g., babelfish_example.py) and remove any cached .pyc files.
error Unknown code: us
cause Country constructor expects uppercase ISO 3166-1 alpha-2 codes.
fix
Use uppercase: Country('US')
gotcha Country and Language objects use enums internally but are not directly mutable after creation. Trying to set attributes may raise AttributeError.
fix Create a new object if you need a different code.
deprecated pkg_resources usage was removed in 0.6.0. If you rely on package resources via babelfish, you may need to adjust.
fix Use importlib.resources as replacement if needed.
gotcha Country codes are case-sensitive internally. 'us' is not the same as 'US' for direct instantiation; use upper-case strings.
fix Always use uppercase codes (e.g., 'US', 'GB').

Basic usage: create Country and Language objects from codes and access properties.

from babelfish import Country, Language

# Look up by code
c = Country('US')
print(c.name)  # United States

l = Language('eng')
print(l.name)  # English

# From alpha3 code (three-letter)
print(Country.fromalpha3('USA'))  # Country('US')