Add translation capability, offer English and French as options
This commit is contained in:
parent
54a6be835f
commit
f578d562e6
17 changed files with 490 additions and 138 deletions
42
bouquin/strings.py
Normal file
42
bouquin/strings.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue