Skip to main content

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 translated text for the given dot path
  @method tr(path: str, **params) -> str

  # Get the raw message for the given dot path
  @method get_raw_message(path: str) -> Union[str, None]

  # Format the given raw message with the given params
  @method format_raw_message(message: 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.

Metadata

Release files for i18nx 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for i18nx 1.3.0
File Size Uploaded Provenance Actions
i18nx-1.3.0.tar.gz 4.8 kB Without provenance Details for i18nx-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for i18nx 1.3.0
File Interpreter ABI Platform Actions
i18nx-1.3.0-py3-none-any.whl Python 3 none any Details for i18nx-1.3.0-py3-none-any.whl

Total release size: 9.8 kB

Release files / i18nx-1.3.0.tar.gz

Download URL i18nx-1.3.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c1b40379773cb48f4ad57aa35aae42783f8001f866287dfbbe0357abc8b284da
BLAKE2b-256 checksum
How to use checksums
4f49f49c6e39a51fcfaa200f21195ad94b70c6ff691ecdaae02aa6027c710a76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / i18nx-1.3.0-py3-none-any.whl

Download URL i18nx-1.3.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5afe786f5ff36a5a75a0ca6edc404b670bdacc071b7a3adf63cb917fb9b2b9f
BLAKE2b-256 checksum
How to use checksums
1577cb8d78a845d48fdd624bc9741337c2e59a95af8d5e8e5efbb937ae0bf261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for i18nx 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page