Classes
MessageCatalog
5
▼
Manages translation catalogs from JSON files.
Usage::
catalog = MessageCatalog("locales")
…
MessageCatalog
5
▼
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).
add_directory
1
▼
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.
load
1
dict[str, str]
▼
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…
translate
3
str
▼
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.
clear
0
▼
def clear(self) -> None
Internal Methods 1 ▼
__init__
1
▼
__init__
1
▼
def __init__(self, directory: str | Path = 'locales') -> None
Parameters
| Name | Type | Description |
|---|---|---|
directory |
— |
Default:'locales'
|