Skip to main content

Python library for loading localizations with dot access and pluralization.

Project description

PyPI version License

doti18n
Type-safe localization library for Python.
Access YAML, JSON, XML and TOML translations using dot-notation.

Overview

doti18n allows you to replace string-based dictionary lookups with intuitive object navigation. Instead of locales['en']['messages']['error'], just write locales["en"].messages.error.

It focuses on Developer Experience (DX) by providing a CLI tool to generate .pyi stubs. This enables IDE autocompletion and allows static type checkers (mypy, pyright) to catch missing keys at build time.

Key Features

  • Dot-Notation: Access nested keys via attributes (data.key) and lists via indices (items[0]).
  • Type Safety: Generate stubs to get full IDE support and catch typos instantly.
  • Advanced ICUMF: Full support for ICU Message Format including nested select, plural, and custom formatters.
  • Pluralization: Robust support powered by Babel.
  • Format Agnostic: Supports YAML, JSON, XML and TOML out of the box.
  • Safety Modes:
    • Strict: Raises exceptions for missing keys (good for dev/test).
    • Non-strict: Returns a safe wrapper and logs warnings (good for production).
  • Fallback: Automatically falls back to the default locale if a key is missing.

Installation

pip install doti18n

If you use YAML files:

pip install doti18n[yaml]

Usage

1. Create a localization file (locales/en.yaml):

__macros__: # Define a section with macros
    gender: {gender, select, male {He} female {She} other {They}}

greeting: "Hello {}!"
farewell: "Goodbye $name!"

# Using macros
user_action: "@gender uploaded a new photo."
user_status: "@gender is currently online."

items:
    - name: "Item 1"
    - name: "Item 2"

# Basic key-based pluralization
notifications:
    one: "You have {count} new notification."
    other: "You have {count} new notifications."

# Complex ICU Message Format (Nesting + Select + Plural)
loot_msg: |
  {hero} found {type, select,
      weapon {{count, plural, one {a legendary sword} other {# rusty swords}}}
      potion {{count, plural, one {a healing potion} other {# healing potions}}}
      other {{count} items}
  } in the chest.

2. Access it in Python:

from doti18n import LocaleData

# Initialize (loads and caches data)
i18n = LocaleData("locales")
en = i18n["en"]

# 1. Standard formatting (Python-style)
print(en.greeting("John"))              # Output: Hello John!

# 2. Variable formatting (Shell-style)
print(en.farewell(name="Alice"))        # Output: Goodbye Alice!

# 3. Raw strings and graceful handling
print(en.farewell)                      # Output: Goodbye $name! (Raw string)
print(en.farewell())                    # Output: Goodbye ! (Missing var handled)

# 4. Using strings with macros
print(en.user_action(gender="male"))    # Output: He uploaded a new photo.
print(en.user_status(gender="female"))  # Output: She is currently online.

# 5. List access
print(en.items[0].name)                 # Output: Item 1

# 6. Basic Pluralization
print(en.notifications(1))              # Output: You have 1 new notification.

# 7. Advanced ICUMF Logic
# "weapon" branch -> "one" sub-branch
print(en.loot_msg(hero="Arthur", type="weapon", count=1))
# Output: Arthur found a legendary sword in the chest.

# "potion" branch -> "other" sub-branch
print(en.loot_msg(hero="Merlin", type="potion", count=5))
# Output: Merlin found 5 healing potions in the chest.

CLI & Type Safety

doti18n comes with a CLI to generate type stubs (.pyi).

Why use it?

  1. Autocompletion: Your IDE will suggest available keys as you type.
  2. Validation: Static analysis tools will flag errors if you try to access a key that doesn't exist.
  3. Deep ICUMF Introspection: The generator parses complex ICUMF strings (like the loot_msg example above) and creates precise function signatures.
    • Example: For loot_msg, it generates: def loot_msg(self, *, hero: str, type: str, count: int) -> str.
    • Your IDE will tell you exactly which arguments are required, even for deeply nested logic.

Commands:

# Generate stubs for all files in 'locales/' (default lang: en)
python -m doti18n stub locales/

# Generate stubs with a specific default language
python -m doti18n stub locales/ -lang fr

# Clean up generated stubs
python -m doti18n stub --clean

Note: Run this inside your virtual environment to ensure stubs are generated for the installed package.

Project Status

Alpha Stage: The API is stable but may evolve before the 1.0.0 release. Feedback and feature requests are highly appreciated!

Documentation

Documentation is available at:
https://darkj3suss.github.io/doti18n/

License

MIT License. See LICENSE for details.

Contact

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

doti18n-0.8.4.tar.gz (41.0 kB view details)

Uploaded Source

Built Distribution

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

doti18n-0.8.4-py3-none-any.whl (58.0 kB view details)

Uploaded Python 3

File details

Details for the file doti18n-0.8.4.tar.gz.

File metadata

  • Download URL: doti18n-0.8.4.tar.gz
  • Upload date:
  • Size: 41.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for doti18n-0.8.4.tar.gz
Algorithm Hash digest
SHA256 ca2de643be78044af4ac58aa534ac03a605ad57b82221c5e34be8c9255dc15f6
MD5 0c3796d9a78adab9ed355cb0eef01227
BLAKE2b-256 de5a1c4e55573585d26d0563bd618774ed6c94e3bcfbd62a6601bafa78170f07

See more details on using hashes here.

Provenance

The following attestation bundles were made for doti18n-0.8.4.tar.gz:

Publisher: python-publish.yml on darkj3suss/doti18n

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file doti18n-0.8.4-py3-none-any.whl.

File metadata

  • Download URL: doti18n-0.8.4-py3-none-any.whl
  • Upload date:
  • Size: 58.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for doti18n-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3323edcb11e98e4b836917c470c1c5159b161d4fd0dacc3f680fe1491076fd76
MD5 5132d91c25e5f48005c94edd953fd671
BLAKE2b-256 4a41bede726793131562be7e0a16046cd0ae7292941061d72bfd6e810142f123

See more details on using hashes here.

Provenance

The following attestation bundles were made for doti18n-0.8.4-py3-none-any.whl:

Publisher: python-publish.yml on darkj3suss/doti18n

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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