-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalisation.py
More file actions
144 lines (115 loc) · 4.16 KB
/
localisation.py
File metadata and controls
144 lines (115 loc) · 4.16 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import json
import logging
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, Iterable, List
LOCALISATION_DIR = Path(__file__).resolve().parent / "localisation"
_LOGGER = logging.getLogger(__name__)
def normalize_language(lang: str) -> str:
cleaned = (lang or "").strip().lower().replace("-", "_")
return cleaned
def language_base(lang: str) -> str:
cleaned = normalize_language(lang)
if not cleaned:
return ""
return cleaned.split("_", 1)[0]
def _language_candidates(lang: str) -> Iterable[str]:
cleaned = normalize_language(lang)
if not cleaned:
return ["en"]
base = language_base(cleaned)
candidates = [cleaned]
if base and base != cleaned:
candidates.append(base)
if "en" not in candidates:
candidates.append("en")
return candidates
def _load_locale_file(language: str) -> Dict[str, Any]:
path = LOCALISATION_DIR / language / "strings.json"
if not path.exists():
return {}
with path.open("r", encoding="utf-8") as handle:
return json.load(handle)
@lru_cache(maxsize=None)
def _merged_localisation(lang: str) -> Dict[str, Dict[str, str]]:
merged: Dict[str, Dict[str, str]] = {
"strings": {},
"gear_type_labels": {},
"relation_labels": {},
}
for code in reversed(list(_language_candidates(lang))):
data = _load_locale_file(code)
for key in merged:
merged[key].update(data.get(key, {}))
_warn_missing_strings(lang)
return merged
def tr(lang: str, key: str) -> str:
return _merged_localisation(lang)["strings"].get(key, key)
def gear_type_label(gear_type: str, lang: str) -> str:
return _merged_localisation(lang)["gear_type_labels"].get(gear_type, gear_type)
def relation_label(relation: str, lang: str) -> str:
return _merged_localisation(lang)["relation_labels"].get(relation, relation)
def available_languages() -> List[str]:
if not LOCALISATION_DIR.exists():
return ["en"]
codes = []
for entry in LOCALISATION_DIR.iterdir():
if not entry.is_dir():
continue
if (entry / "strings.json").exists():
codes.append(entry.name)
return sorted(codes) or ["en"]
def resolve_language(lang: str) -> str:
for code in _language_candidates(lang):
if (LOCALISATION_DIR / code / "strings.json").exists():
return code
return "en"
def language_display_name(lang: str) -> str:
normalized = normalize_language(lang)
data = _load_locale_file(normalized)
name = data.get("strings", {}).get("language_name")
if name:
return name
base = language_base(normalized)
if base and base != normalized:
base_name = _load_locale_file(base).get("strings", {}).get("language_name")
if base_name:
return base_name
return normalized or "en"
def resolve_readme_path(lang: str) -> Path:
normalized = normalize_language(lang)
base = language_base(normalized)
candidates = []
for code in (normalized, base):
if code:
candidates.append(LOCALISATION_DIR / code / "README.md")
for code in (normalized, base):
if code:
candidates.append(LOCALISATION_DIR.parent / f"README.{code}.md")
candidates.append(LOCALISATION_DIR.parent / "README.md")
for path in candidates:
if path.exists():
return path
return LOCALISATION_DIR.parent / "README.md"
@lru_cache(maxsize=None)
def _missing_string_keys(lang: str) -> List[str]:
normalized = normalize_language(lang)
if not normalized:
return []
if normalized == "en":
return []
en_strings = _load_locale_file("en").get("strings", {})
if not en_strings:
return []
locale_strings = _load_locale_file(normalized).get("strings", {})
if not locale_strings:
return []
return sorted(set(en_strings.keys()) - set(locale_strings.keys()))
def _warn_missing_strings(lang: str) -> None:
missing = _missing_string_keys(lang)
if missing:
_LOGGER.warning(
"Missing localisation strings for %s: %s",
normalize_language(lang),
", ".join(missing),
)