ข้ามไปที่เนื้อหา

i18n

i18n

Centralized internationalization engine for UI strings.

Provides a tr() accessor, a language-change signal, and JSON-based translation loading. Mirrors the theme engine pattern in theme.py.

current_language

current_language()

Returns the code of the currently active UI language.

Source code in src/constants/i18n.py
def current_language() -> str:
    """Returns the code of the currently active UI language."""
    return _current_language

set_language

set_language(code)

Switches the active UI language and emits the changed signal.

Source code in src/constants/i18n.py
def set_language(code: str) -> None:
    """Switches the active UI language and emits the changed signal."""
    global _current_language  # noqa: PLW0603
    valid_codes = {c for c, *_ in UI_LANGUAGES}
    if code not in valid_codes:
        logger.warning("Unknown language '%s', ignoring.", code)
        return
    if code == _current_language:
        return
    _current_language = code
    _load_translations(code)
    language_changed.emit(code)

_set_initial_language

_set_initial_language(code)

Sets the language at startup without emitting a signal.

Source code in src/constants/i18n.py
def _set_initial_language(code: str) -> None:
    """Sets the language at startup without emitting a signal."""
    global _current_language  # noqa: PLW0603
    valid_codes = {c for c, *_ in UI_LANGUAGES}
    if code in valid_codes:
        _current_language = code
    _load_translations(_current_language)

tr

tr(key, **kwargs)

Returns the translated string for key in the current language.

Supports Python format syntax: tr("msg", count=5) replaces {count} in the translated template. Falls back to key itself when no translation is found.

Source code in src/constants/i18n.py
def tr(key: str, **kwargs: object) -> str:
    """Returns the translated string for *key* in the current language.

    Supports Python format syntax: ``tr("msg", count=5)`` replaces
    ``{count}`` in the translated template.  Falls back to *key* itself
    when no translation is found.
    """
    template = _translations.get(key, key)
    if kwargs:
        try:
            return template.format(**kwargs)
        except (KeyError, IndexError):
            logger.warning("Format error for key '%s': %s", key, kwargs)
            return template
    return template

_load_translations

_load_translations(code)

Reads the JSON translation file for code into module state.

Source code in src/constants/i18n.py
def _load_translations(code: str) -> None:
    """Reads the JSON translation file for *code* into module state."""
    global _translations  # noqa: PLW0603
    json_path = _TRANSLATIONS_DIR / f"{code}.json"
    if not json_path.exists():
        logger.warning("Translation file not found: %s", json_path)
        _translations = {}
        return
    try:
        with json_path.open(encoding="utf-8") as fh:
            _translations = json.load(fh)
    except (json.JSONDecodeError, OSError) as exc:
        logger.error("Failed to load translations from %s: %s", json_path, exc)
        _translations = {}