Skip to main content

Lightweight i18n for Python

Project description

I18n for Python

pypi version Build status Test Coverage Buy me a beer

Lightweight i18n for Python.

Install

pip install i18nx

Usage

from i18nx import I18n

i18n = I18n(
  locale = 'en',
  fallback = 'fr',
  translations = {
    'en': { 'message': { 'hello': 'Hello World!' } },
    'fr': { 'message': { 'hello': 'Bonjour le monde !' } },
  },
)

print(i18n.tr("message.hello")) # Hello World!

Message Format Syntax

Interpolation

i18nx use the Mustache like placeholders {} syntax for interpolation.

from i18nx import I18n

i18n = I18n(
  locale = 'en',
  fallback = 'fr',
  translations = {
    'en': { 'message': { 'hello': 'Hello {name}!' } },
    'fr': { 'message': { 'hello': 'Bonjour {name} !' } },
  },
)

print(i18n.tr("message.hello", name = 'Mario')) # Hello Mario!

Pluralization

Use a pipe | separator in combination with the param count to define plurals on the locale translations.

from i18nx import I18n

i18n = I18n(
  locale = 'en',
  fallback = 'fr',
  translations = {
    'en': {
      'car': 'car | cars',
      'apple': 'no apples | one apple | {count} apples',
    },
  },
)

print(i18n.tr("car")) # 'car'
print(i18n.tr("car", count = 0)) # 'car'
print(i18n.tr("car", count = 1)) # 'car'
print(i18n.tr("car", count = 2)) # 'cars'
print(i18n.tr("apple", count = 0)) # 'no apples'
print(i18n.tr("apple", count = 1)) # 'one apple'
print(i18n.tr("apple", count = 15)) # '15 apples'

List of Messages

from i18nx import I18n

i18n = I18n(
  locale = 'en',
  fallback_locale = 'fr',
  translations = {
    'en': {
      "greetings": [
        "Hey {firtname}!",
        "Hi {firtname}!",
      ],
    },
  },
)

print(i18n.tr("greetings.0", firtname = 'Mario')) # 'Hey Mario!'
print(i18n.tr("greetings.1", firtname = 'Mario')) # 'Hi Mario!'

I18n API

interface class I18n:
  constructor(locale: str, fallback_locale: str, translations: Dict[str, Dict[str, Any]], show_warning = False)

  # Active locale
  @property locale: str    

  # Fallback locale
  @property fallback_locale: str

  # Available locales
  @getter available_locales: List[str]
  
  # Raw translations object for the active locale
  @getter raw: dict

  # Get the translate text for the given dot path
  @method tr(path: str, **params) -> str

Development Setup

  1. Install Nix Package Manager

  2. Install direnv with your OS package manager

  3. Hook it direnv into your shell

  4. At the top-level of your project run:

    direnv allow
    

    The next time your launch your terminal and enter the top-level of your project, direnv will check for changes.

Scripts

# run tests
make test

# run tests with coverage
make coverage

# run linter
make lint

# run build process
make dist

# publish
make publish

License

Under the MIT license. See LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

i18nx-1.0.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

i18nx-1.0.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file i18nx-1.0.0.tar.gz.

File metadata

  • Download URL: i18nx-1.0.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for i18nx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a3728d74c0ab986dc11bef56560c22d0a39677048ad8c4003d4c3f6cffa48fad
MD5 4b5ebf6f22e52a0c69040b9b6f7cc986
BLAKE2b-256 993dc5ceb12cd3a074f09f1b24f64e6fdf3848d15aa620be90ccf49feb4a0333

See more details on using hashes here.

File details

Details for the file i18nx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: i18nx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for i18nx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 deb9ad8974e2a2234de7a8f85b01794a8a417d1141593845452c08637e4a53c4
MD5 e23e0cd634a3593d8461651f175aad40
BLAKE2b-256 0151ddbce2e3e2d82be627e6984e44e3dadd010f9ef6af08ec42c033d7b4d084

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page