Modern-i18n is a modern library for internationalization (i18n) in Python projects.
Project description
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.pyfile, 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
localedirectory, 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 -> Ключ не существует
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file modern_i18n-1.0.0.tar.gz.
File metadata
- Download URL: modern_i18n-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6cadbb8ec8844797cab8ede26fb4feaa9155c0d3ef23c2d175b4ee07905efe5
|
|
| MD5 |
d69c7ba65d4b123c719a4e7ad02438b5
|
|
| BLAKE2b-256 |
977dcf3fba0708afb99560b8fdfbae61cc39b011f61241d706500f2cb7acd50f
|