-
Notifications
You must be signed in to change notification settings - Fork 842
Expand file tree
/
Copy pathi18n.py
More file actions
69 lines (56 loc) · 1.85 KB
/
Copy pathi18n.py
File metadata and controls
69 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Author: Junjun
# Date: 2025/12/11
# i18n.py
import json
from pathlib import Path
from typing import Dict
i18n_list = ["en", "zh"]
# placeholder prefix(trans key prefix)
PLACEHOLDER_PREFIX = "PLACEHOLDER_"
# default lang
DEFAULT_LANG = "en"
LOCALES_DIR = Path(__file__).parent / "locales"
_translations_cache: Dict[str, Dict[str, str]] = {}
def load_translation(lang: str) -> Dict[str, str]:
"""Load translations for the specified language from a JSON file"""
if lang in _translations_cache:
return _translations_cache[lang]
file_path = LOCALES_DIR / f"{lang}.json"
if not file_path.exists():
if lang == DEFAULT_LANG:
raise FileNotFoundError(f"Default language file not found: {file_path}")
# If the non-default language is missing, fall back to the default language
return load_translation(DEFAULT_LANG)
try:
with open(file_path, "r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, dict):
raise ValueError(f"Translation file {file_path} must be a JSON object")
_translations_cache[lang] = data
return data
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in {file_path}: {e}")
# group tags
tags_metadata = [
{
"name": "Datasource",
"description": f"{PLACEHOLDER_PREFIX}ds_api"
},
{
"name": "system_user",
"description": f"{PLACEHOLDER_PREFIX}system_user_api"
},
{
"name": "system_ws",
"description": f"{PLACEHOLDER_PREFIX}system_ws_api"
},
{ "name": "Table Relation",
"description": f"{PLACEHOLDER_PREFIX}tr_api"
},
{
"name": "Data Permission",
"description": f"{PLACEHOLDER_PREFIX}per_api"
},
]
def get_translation(lang: str) -> Dict[str, str]:
return load_translation(lang)