Skip to main content

modern-i18n

Modern-i18n is a modern library for internationalization (i18n) in Python projects.

Installation

pip install modern-i18n

Project Structure

Here is an example of the typical project structure after setting up the modern-i18n library:

project/
├── i18n/                # 📁 Internationalization module
│   └── config.py        # 📄 Configuration file: default language, path to translations, etc.
│
├── locale/              # 📁 Directory with translations (localizations)
│   ├── en/              # 📁 English locale
│   │   └── translation.json  # 📄 Translation file for English
│   └── ru/              # 📁 Russian locale
│       └── translation.json  # 📄 Translation file for Russian
│
└── example.py           # 📄 Example of using the translation function

Configuration

All library settings are located in the i18n/config.py file. Here is an example of a basic configuration:

import os
from pathlib import Path

from modern_i18n.i18n import I18n

project_root = Path(__file__).resolve().parent.parent
LOCALE_PATH = os.path.join(project_root, 'locale')

config = {
    'locale_path': LOCALE_PATH,  # Path to the directory with translations
    'locales': ['ru', 'en'],  # List of available languages
    'default_locale': 'ru',  # Default language
    'default_encoding': 'UTF-8'  # Default encoding for translation files is 'UTF-8'
}

i18n = I18n(config)
t = i18n.translate
  • In the i18n/config.py file, you can configure main parameters such as the list of supported languages, the default language, the path to the translation directory, and additional options.
  • All translations are stored in JSON format inside the locale directory, where each locale has its own folder.

Examples of translation.json content

JSON files store key-value pairs, where the key is the original phrase, and the value is its translation into the target language.

locale/en/translation.json:

{
  "Привет": "Hello!",
  "Итого {a}+{b}={total}": "Total {a}+{b}={total}."
}

locale/ru/translation.json:

{
  "Привет": "Привет!",
  "Итого {a}+{b}={total}": "Итого {a}+{b}={total}."
}

⚠️ Note that the original keys must be the same across all language files to ensure correct matching.

Usage

Example of basic usage of the translation function t:

from i18n.config import t

print(t('Привет'))  # -> Привет!
# Translation with specified locale
print(t('Привет', locale='en'))  # -> Hello!
# Translation with parameters
print(t('Итого {a}+{b}={total}', a=5, b=10, total=15))  # -> Итого 5+10=15.
print(t('Итого {a}+{b}={total}', locale='en', a=5, b=10, total=15))  # -> Total 5+10=15.
print(t('Ключ не существует'))  # Warning -> Ключ не существует

Metadata

Release files for modern-i18n 1.0.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 modern-i18n 1.0.0
File Size Uploaded Provenance Actions
modern_i18n-1.0.0.tar.gz 4.4 kB Without provenance Details for modern_i18n-1.0.0.tar.gz

Release files / modern_i18n-1.0.0.tar.gz

Download URL modern_i18n-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a6cadbb8ec8844797cab8ede26fb4feaa9155c0d3ef23c2d175b4ee07905efe5
BLAKE2b-256 checksum
How to use checksums
977dcf3fba0708afb99560b8fdfbae61cc39b011f61241d706500f2cb7acd50f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for modern-i18n 1.0.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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file No provenance

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