Load translations for the specified language from a JSON file
(lang: str)
| 18 | |
| 19 | |
| 20 | def load_translation(lang: str) -> Dict[str, str]: |
| 21 | """Load translations for the specified language from a JSON file""" |
| 22 | if lang in _translations_cache: |
| 23 | return _translations_cache[lang] |
| 24 | |
| 25 | file_path = LOCALES_DIR / f"{lang}.json" |
| 26 | if not file_path.exists(): |
| 27 | if lang == DEFAULT_LANG: |
| 28 | raise FileNotFoundError(f"Default language file not found: {file_path}") |
| 29 | # If the non-default language is missing, fall back to the default language |
| 30 | return load_translation(DEFAULT_LANG) |
| 31 | |
| 32 | try: |
| 33 | with open(file_path, "r", encoding="utf-8") as f: |
| 34 | data = json.load(f) |
| 35 | if not isinstance(data, dict): |
| 36 | raise ValueError(f"Translation file {file_path} must be a JSON object") |
| 37 | _translations_cache[lang] = data |
| 38 | return data |
| 39 | except json.JSONDecodeError as e: |
| 40 | raise ValueError(f"Invalid JSON in {file_path}: {e}") |
| 41 | |
| 42 | |
| 43 | # group tags |
no test coverage detected