i18nice

0.16.0 · active · verified Thu Apr 16

i18nice is a Python library that provides internationalization (i18n) functionality, largely inspired by the Rails i18n library. It simplifies translation management for Python 3 applications by supporting translation files (JSON, YAML), placeholders, pluralization, and fallback locales. The current version is 0.16.0, and it maintains a relatively active release cadence with minor and patch updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `i18nice` by adding a translation path, setting the locale, and performing basic string translation with placeholders and pluralization using a simple JSON file.

import i18n
import os

# Create a dummy translation directory and file
# In a real application, these would be pre-existing
if not os.path.exists('translations'):
    os.makedirs('translations')
with open('translations/app.en.json', 'w') as f:
    f.write('{
  "greeting": "Hello %{name}!",
  "mails": {
    "zero": "You have no mails.",
    "one": "You have one mail.",
    "other": "You have %{count} mails."
  }
}')

# Configure i18n to load translations from the 'translations' directory
i18n.load_path.append('translations')
i18n.set('locale', 'en')

# Translate a simple string with a placeholder
message = i18n.t('app.greeting', name='World')
print(message) # Expected: Hello World!

# Translate with pluralization
message_zero = i18n.t('app.mails', count=0)
message_one = i18n.t('app.mails', count=1)
message_many = i18n.t('app.mails', count=5)

print(message_zero) # Expected: You have no mails.
print(message_one)  # Expected: You have one mail.
print(message_many) # Expected: You have 5 mails.

# Clean up dummy file and directory
os.remove('translations/app.en.json')
os.rmdir('translations')

view raw JSON →