Python i18n

0.3.9 · maintenance · verified Wed Apr 15

python-i18n is an easy-to-use internationalization (i18n) library for Python 3, providing functionality largely inspired by Rails i18n. It allows developers to manage translations, pluralization, and placeholders using YAML or JSON files. The current version is 0.3.9, last released in August 2020, indicating a low-activity maintenance status.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `python-i18n` by adding translation file paths, setting the locale, and performing basic translations, including placeholders and pluralization. It assumes translation files (YAML or JSON) are stored in a `locales` directory.

import i18n
import os

# Configure translation path (assuming 'locales' directory exists with translation files)
# Example: locales/en.yml or locales/foo.en.yml
# Create a dummy translation file for the example
if not os.path.exists('locales'):
    os.makedirs('locales')
with open('locales/en.yml', 'w') as f:
    f.write('en:\n  hello: "Hello world!"\n  welcome: "Welcome, %{name}!"\n  mail_number:\n    zero: "You have no mails."\n    one: "You have one mail."
    many: "You have %{count} mails."
')

i18n.load_path.append('./locales')

# Set the default locale
i18n.set('locale', 'en')

# Basic translation from a file (if en.yml exists with 'en: hello: "Hello world!"')
print(i18n.t('hello'))

# Translation with placeholders
print(i18n.t('welcome', name='Bob'))

# Pluralization (requires `count` argument and specific keys in translation file)
print(i18n.t('mail_number', count=0)) # Should output 'You have no mails.'
print(i18n.t('mail_number', count=1)) # Should output 'You have one mail.'
print(i18n.t('mail_number', count=5)) # Should output 'You have 5 mails.'

view raw JSON →