Post-merge / detect-type (push) Successful in 51s
Post-merge / release (push) Successful in 1m14s
Post-merge / validate-commit-msg (push) Successful in 1m14s
Post-merge / vikunja (push) Successful in 1m14s
Post-merge / badges (push) Successful in 1m25s
Post-merge / sync-wiki (push) Successful in 1m53s
Post-merge / configure-repo (push) Successful in 1m20s
Post-merge / publish (push) Successful in 1m21s
39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""Simple i18n for GRM console messages.
|
|
|
|
Set GRM_LANG environment variable to override the default English.
|
|
Supported: en, bg, de, ru, zh, pl.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger("grm")
|
|
|
|
|
|
def _load_translations() -> dict[str, dict[str, str]]:
|
|
try:
|
|
return json.loads((Path(__file__).parent / "translations.json").read_text(encoding="utf-8"))
|
|
except (json.JSONDecodeError, OSError) as e:
|
|
logger.warning("Failed to load translations.json: %s — falling back to English", e)
|
|
return {}
|
|
|
|
|
|
TRANSLATIONS: dict[str, dict[str, str]] = _load_translations()
|
|
|
|
|
|
def _(key: str, **kwargs: object) -> str:
|
|
"""Return a translated string for the given key.
|
|
|
|
Translation is opt-in via the ``GRM_LANG`` environment variable.
|
|
If unset, English is always returned regardless of system locale.
|
|
"""
|
|
lang = os.getenv("GRM_LANG", "en")
|
|
if lang not in ("en", "bg", "de", "ru", "zh", "pl"):
|
|
lang = "en"
|
|
template = TRANSLATIONS.get(key, {}).get(lang, key)
|
|
return template.format(**kwargs)
|