39 lines
1,022 B
Python
39 lines
1,022 B
Python
"""Internationalisation — built-in NL + EN."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
_DIR = Path(__file__).parent.parent / "translations"
|
|
_cache: dict[str, dict] = {}
|
|
_langs: dict[str, dict] = {}
|
|
|
|
|
|
def load():
|
|
_cache.clear()
|
|
_langs.clear()
|
|
if not _DIR.exists():
|
|
return
|
|
for f in sorted(_DIR.glob("*.json")):
|
|
try:
|
|
data = json.loads(f.read_text("utf-8"))
|
|
meta = data.get("_meta", {})
|
|
code = meta.get("code", f.stem)
|
|
_langs[code] = meta
|
|
_cache[code] = {k: v for k, v in data.items() if not k.startswith("_")}
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def available() -> list[dict]:
|
|
if not _langs:
|
|
load()
|
|
return [{"code": k, "name": v.get("name", k), "flag": v.get("flag", "")}
|
|
for k, v in _langs.items()]
|
|
|
|
|
|
def strings(lang: str) -> dict:
|
|
if not _cache:
|
|
load()
|
|
base = dict(_cache.get("en", {}))
|
|
if lang != "en" and lang in _cache:
|
|
base.update(_cache[lang])
|
|
return base
|