50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# resources/code_res/i18n.py
|
||
|
|
from models.database import Database, INTERNAL_DB_PATH
|
||
|
|
|
||
|
|
SUPPORTED_LANGS = ("DE", "EN")
|
||
|
|
CURRENT_LANG = "DE"
|
||
|
|
_CACHE: dict[str, dict[str, str]] = {}
|
||
|
|
_MISSING_WARNED: set[str] = set()
|
||
|
|
|
||
|
|
|
||
|
|
def reload_translations():
|
||
|
|
global _CACHE
|
||
|
|
_CACHE = Database.load_all_translations()
|
||
|
|
|
||
|
|
|
||
|
|
def set_lang(lang_code: str):
|
||
|
|
global CURRENT_LANG
|
||
|
|
code = (lang_code or "DE").upper()
|
||
|
|
CURRENT_LANG = code if code in SUPPORTED_LANGS else "DE"
|
||
|
|
|
||
|
|
|
||
|
|
def get_lang() -> str:
|
||
|
|
return CURRENT_LANG
|
||
|
|
|
||
|
|
|
||
|
|
def next_lang() -> str:
|
||
|
|
idx = SUPPORTED_LANGS.index(CURRENT_LANG)
|
||
|
|
new_lang = SUPPORTED_LANGS[(idx + 1) % len(SUPPORTED_LANGS)]
|
||
|
|
set_lang(new_lang)
|
||
|
|
return new_lang
|
||
|
|
|
||
|
|
|
||
|
|
def t(key: str, *args) -> str:
|
||
|
|
if not _CACHE:
|
||
|
|
reload_translations()
|
||
|
|
|
||
|
|
lang_dict = _CACHE.get(CURRENT_LANG, {})
|
||
|
|
raw = lang_dict.get(key) or _CACHE.get("DE", {}).get(key)
|
||
|
|
|
||
|
|
if raw is None:
|
||
|
|
if key not in _MISSING_WARNED:
|
||
|
|
_MISSING_WARNED.add(key)
|
||
|
|
print(f"[i18n FEHLT] Key '{key}' nicht in {INTERNAL_DB_PATH} gefunden!")
|
||
|
|
return f"{key}: {' | '.join(str(a) for a in args)}" if args else key
|
||
|
|
|
||
|
|
text = raw.replace("\\n", "\n")
|
||
|
|
try:
|
||
|
|
return text.format(*args) if args else text
|
||
|
|
except Exception:
|
||
|
|
return text
|