Module

i18n.catalog

Message catalog — load JSON translation files.

Simple key-value translations from JSON files. Lazy-loaded per locale. Missing key returns the source string with a warning (never breaks rendering).

Classes

MessageCatalog 5
Manages translation catalogs from JSON files. Usage:: catalog = MessageCatalog("locales") …

Manages translation catalogs from JSON files.

Usage::

catalog = MessageCatalog("locales")
catalog.load("es")
msg = catalog.translate("es", "Hello, {name}!", name="Alice")

Methods

add_directory 1
Add an additional locale directory (e.g., from a plugin).
def add_directory(self, directory: str | Path) -> None
Parameters
Name Type Description
directory
load 1 dict[str, str]
Load translations for a locale. Lazy — loads on first access.
def load(self, locale: str) -> dict[str, str]
Parameters
Name Type Description
locale
Returns
dict[str, str]
translate 3 str
Look up a translation, with optional interpolation. Missing keys return the ke…
def translate(self, locale: str, key: str, **kwargs) -> str

Look up a translation, with optional interpolation.

Missing keys return the key itself (never breaks rendering).

Parameters
Name Type Description
locale
key
**kwargs
Returns
str
clear 0
Clear all cached catalogs.
def clear(self) -> None
Internal Methods 1
__init__ 1
def __init__(self, directory: str | Path = 'locales') -> None
Parameters
Name Type Description
directory Default:'locales'