Make locales dynamically detected from the locales dir rather than hardcoded
All checks were successful
CI / test (push) Successful in 2m21s

This commit is contained in:
Miguel Jacq 2025-11-13 14:33:34 +11:00
parent a4e9af4444
commit c18f0f6f36
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
2 changed files with 14 additions and 4 deletions

View file

@ -1,3 +1,8 @@
# 0.2.1.9
* Fix a few small matters identified with tests
* Make locales dynamically detected from the locales dir rather than hardcoded
# 0.2.1.8
* Translate all strings, add French, add locale choice in settings

View file

@ -1,19 +1,24 @@
from importlib.resources import files
import json
_AVAILABLE = ("en", "fr")
# Get list of locales
root = files("bouquin") / "locales"
_AVAILABLE = tuple(
entry.stem
for entry in root.iterdir()
if entry.is_file() and entry.suffix == ".json"
)
_DEFAULT = "en"
strings = {}
translations = {}
def load_strings(current_locale: str) -> None:
global strings, translations
translations = {}
# read json resources from bouquin/locales/*.json
root = files("bouquin") / "locales"
# read in the locales json
for loc in _AVAILABLE:
data = (root / f"{loc}.json").read_text(encoding="utf-8")
translations[loc] = json.loads(data)