Python library for loading localizations with dot access and pluralization.
Project description
Simple and intuitive Python library for loading localizations from YAML, JSON, XML files and accessing them easily using dot notation, with powerful support for plural forms and nested data structures. It also gives you strong DX(Developer Experience) with stubs generation for your localization files.
Description
doti18n provides a convenient way to manage your application's localization strings. By loading data from files, the library allows you to access nested translations using a simple dot syntax (messages.status.online) for dictionary keys and index syntax (items[0]) for list elements. You can combine these for intuitive navigation through complex nested structures (pages[0].title).
Special attention is given to pluralization support using the Babel library, which is critical for correct localization across different languages. An automatic fallback mechanism to the default locale's value is also implemented if a key or path is missing in the requested locale.
The library offers both a forgiving non-strict mode (returning a special wrapper and logging warnings) and a strict mode (raising exceptions) for handling missing paths.
It's designed for ease of use and performance (data is loaded once during initialization and translator objects are cached).
Features
- Loading localization data from YAML, JSON, XML files.
- Intuitive access to nested data structures (dictionaries and lists) using dot notation (
.) for dictionary keys and index notation ([]) for list elements. - Support for combined access paths (
data.list[0].nested_key). - Strict mode (
strict=True) to raise exceptions on missing paths or incorrect usage. - Non-strict mode (default) to return a special
NoneWrapperobject and log a warning on missing paths or incorrect usage. - Pluralization support for count-dependent strings (requires
Babel). - Automatic fallback to the default locale if a key/path is missing in the current locale.
- Caching of loaded data and translator objects for efficient access.
Installation
doti18n is available on PyPI.
Instaling:
pip install doti18n
Usage
Here's a basic example of how to use doti18n:
Let's say you have a YAML file like this:
# locales/en.yaml
greeting: "Hello {}!"
farewell: "Goodbye $name!"
items:
- name: "Item 1"
- name: "Item 2"
notifications:
one: "You have {count} new notification."
other: "You have {count} new notifications."
You can load and use it as follows:
# Import main class
from doti18n import LocaleData
# Create a LocaleData instance
i18n = LocaleData("locales")
# Access translations
print(i18n["en"].greeting("John")) # Output: Hello John!
print(i18n["en"].farewell(name="Alice")) # Output: Goodbye Alice!
print(i18n["en"].farewell) # Output: Goodbye $name!
print(i18n["en"].farewell()) # Output: Goodbye !
print(i18n["en"].items[0].name) # Output: Item 1
print(i18n["en"].notifications(1)) # Output: You have 1 new notification.
print(i18n["en"].notifications(5)) # Output: You have 5 new notifications.
# You also can get LocaleTranslator object directly
t = i18n["en"]
print(t.notifications(2)) # Output: You have 2 new notifications.
# Even more, you can do this for any level of nesting
it = t.items
print(it[1].name) # Output: Item 2
CLI
Stub generator is available via the CLI command doti18n stub - helper that creates type stubs for your translations.
What it does and why you want it:
- Scans all locale files in the provided directory and collects the keys structure.
- Generates
doti18n/__init__.pyiwith classes and method signatures for each locale (namespaces, keys and formatted-string signatures). - Provides IDE autocompletion and helps type-checkers (mypy, Pyright) catch typos in translation keys - makes working with localizations safer and more convenient.
Usage examples:
python -m doti18n stub locales/ # generate stubs (default locale = en)
python -m doti18n stub locales/ -lang fr # set another default locale
python -m doti18n stub --clean # remove previously generated stubs
Note: the command will warn if run outside a virtual environment - it's recommended to run it inside a venv to avoid dependency conflicts.
Project Status
This project is in an early stage of development (Alpha). The API may change in future versions before reaching a stable (1.0.0) release. Any feedback and suggestions are welcome!
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
If you have questions, feel free to open an issue on GitHub. Or you can message me on Telegram
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file doti18n-0.4.0.tar.gz.
File metadata
- Download URL: doti18n-0.4.0.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b62cece8c9f46fbafb3112910d67611ffc48ac8941a8a7e34fea9c32b8e3c492
|
|
| MD5 |
4cb7e353ab5037715f8b073ab2b0ee1a
|
|
| BLAKE2b-256 |
3be0cebcb49ede2efe8b6dbd30c1d9fed61e4f71b33bfdf2cd0e80719c5002d6
|
Provenance
The following attestation bundles were made for doti18n-0.4.0.tar.gz:
Publisher:
python-publish.yml on darkj3suss/doti18n
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doti18n-0.4.0.tar.gz -
Subject digest:
b62cece8c9f46fbafb3112910d67611ffc48ac8941a8a7e34fea9c32b8e3c492 - Sigstore transparency entry: 771229519
- Sigstore integration time:
-
Permalink:
darkj3suss/doti18n@12a7ecd4acda1772688aa10088ddb50fde13618b -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/darkj3suss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@12a7ecd4acda1772688aa10088ddb50fde13618b -
Trigger Event:
push
-
Statement type:
File details
Details for the file doti18n-0.4.0-py3-none-any.whl.
File metadata
- Download URL: doti18n-0.4.0-py3-none-any.whl
- Upload date:
- Size: 32.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
280914f8adc6e6b5b4fa1c24865909533182f7e4f9cd7804c280ca5d2f10454c
|
|
| MD5 |
2fefaec86e9fbf2a8dd247c65846b1b8
|
|
| BLAKE2b-256 |
cd1124dfa6731fbb438c5261a91bf5770837a668340d152f0e9dfb56249d0ee0
|
Provenance
The following attestation bundles were made for doti18n-0.4.0-py3-none-any.whl:
Publisher:
python-publish.yml on darkj3suss/doti18n
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
doti18n-0.4.0-py3-none-any.whl -
Subject digest:
280914f8adc6e6b5b4fa1c24865909533182f7e4f9cd7804c280ca5d2f10454c - Sigstore transparency entry: 771229528
- Sigstore integration time:
-
Permalink:
darkj3suss/doti18n@12a7ecd4acda1772688aa10088ddb50fde13618b -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/darkj3suss
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@12a7ecd4acda1772688aa10088ddb50fde13618b -
Trigger Event:
push
-
Statement type: