Add translation capability, offer English and French as options

This commit is contained in:
Miguel Jacq 2025-11-12 13:58:58 +11:00
parent 54a6be835f
commit f578d562e6
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
17 changed files with 490 additions and 138 deletions

42
bouquin/strings.py Normal file
View file

@ -0,0 +1,42 @@
from importlib.resources import files
import json
_AVAILABLE = ("en", "fr")
_DEFAULT = "en"
strings = {}
translations = {}
def load_strings(current_locale: str | None = None) -> None:
global strings, translations
translations = {}
# read json resources from bouquin/locales/*.json
root = files("bouquin") / "locales"
for loc in _AVAILABLE:
data = (root / f"{loc}.json").read_text(encoding="utf-8")
translations[loc] = json.loads(data)
# Load in the system's locale if not passed in somehow from settings
if not current_locale:
try:
from PySide6.QtCore import QLocale
current_locale = QLocale.system().name().split("_")[0]
except Exception:
current_locale = _DEFAULT
if current_locale not in translations:
current_locale = _DEFAULT
base = translations[_DEFAULT]
cur = translations.get(current_locale, {})
strings = {k: (cur.get(k) or base[k]) for k in base}
def translated(k: str) -> str:
return strings.get(k, k)
_ = translated