Skip to content

Commit da27dd0

Browse files
committed
normalisation i1_n pour engine
1 parent 52f7168 commit da27dd0

3 files changed

Lines changed: 98 additions & 646 deletions

File tree

Core/engines_loader/registry.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,87 @@
2626
# Keep live engine instances to support dynamic interactions (e.g., i18n refresh)
2727
_INSTANCES: dict[str, CompilerEngine] = {}
2828

29+
# Language code aliases for normalization
30+
_LANG_ALIASES: dict[str, str] = {
31+
"en-us": "en",
32+
"en_gb": "en",
33+
"en-uk": "en",
34+
"fr-fr": "fr",
35+
"fr_ca": "fr",
36+
"fr-ca": "fr",
37+
"pt-br": "pt-BR",
38+
"pt_br": "pt-BR",
39+
"zh": "zh-CN",
40+
"zh_cn": "zh-CN",
41+
"zh-cn": "zh-CN",
42+
}
43+
44+
45+
def normalize_language_code(code: Optional[str]) -> str:
46+
"""Normalize language code with fallback chain.
47+
48+
Returns normalized code or 'en' as ultimate fallback.
49+
"""
50+
if not code:
51+
return "en"
52+
53+
try:
54+
raw = str(code)
55+
low = raw.lower().replace("_", "-")
56+
mapped = _LANG_ALIASES.get(low, raw)
57+
58+
# Candidate order: mapped -> base (before '-') -> exact lower -> exact raw -> 'en'
59+
candidates = []
60+
if mapped not in candidates:
61+
candidates.append(mapped)
62+
63+
base = None
64+
try:
65+
if "-" in mapped:
66+
base = mapped.split("-", 1)[0]
67+
elif "_" in mapped:
68+
base = mapped.split("_", 1)[0]
69+
except Exception:
70+
base = None
71+
72+
if base and base not in candidates:
73+
candidates.append(base)
74+
if low not in candidates:
75+
candidates.append(low)
76+
if raw not in candidates:
77+
candidates.append(raw)
78+
if "en" not in candidates:
79+
candidates.append("en")
80+
81+
return candidates[0] if candidates else "en"
82+
except Exception:
83+
return "en"
84+
85+
86+
def resolve_language_code(gui, tr: Optional[dict]) -> str:
87+
"""Resolve language code from translations metadata or GUI preferences.
88+
89+
Returns normalized language code.
90+
"""
91+
code = None
92+
93+
try:
94+
if isinstance(tr, dict):
95+
meta = tr.get("_meta", {})
96+
code = meta.get("code") if isinstance(meta, dict) else None
97+
except Exception:
98+
code = None
99+
100+
if not code:
101+
try:
102+
pref = getattr(gui, "language_pref", getattr(gui, "language", "System"))
103+
if isinstance(pref, str) and pref != "System":
104+
code = pref
105+
except Exception:
106+
pass
107+
108+
return normalize_language_code(code)
109+
29110

30111
def unregister(eid: str) -> None:
31112
"""Unregister an engine id and its tab mapping if present."""

ENGINES/cx_freeze/engine.py

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -969,65 +969,11 @@ def _browse_icon():
969969
def apply_i18n(self, gui, tr: dict[str, str]) -> None:
970970
"""Apply engine-local i18n from ENGINES/cx_freeze/languages/*.json independent of app languages."""
971971
try:
972-
# Resolve language code preference
973-
code = None
974-
try:
975-
if isinstance(tr, dict):
976-
meta = tr.get("_meta", {})
977-
code = meta.get("code") if isinstance(meta, dict) else None
978-
except Exception:
979-
code = None
980-
if not code:
981-
try:
982-
# Try GUI preferences
983-
pref = getattr(
984-
gui, "language_pref", getattr(gui, "language", "System")
985-
)
986-
if isinstance(pref, str) and pref != "System":
987-
code = pref
988-
except Exception:
989-
pass
990-
# Fallback
991-
if not code:
992-
code = "en"
993-
# Normalize codes and build robust fallback candidates
994-
raw = str(code)
995-
low = raw.lower().replace("_", "-")
996-
aliases = {
997-
"en-us": "en",
998-
"en_gb": "en",
999-
"en-uk": "en",
1000-
"fr-fr": "fr",
1001-
"fr_ca": "fr",
1002-
"fr-ca": "fr",
1003-
"pt-br": "pt-BR",
1004-
"pt_br": "pt-BR",
1005-
"zh": "zh-CN",
1006-
"zh_cn": "zh-CN",
1007-
"zh-cn": "zh-CN",
1008-
}
1009-
mapped = aliases.get(low, raw)
1010-
# Candidate order: mapped -> base (before '-') -> exact lower -> exact raw -> 'en'
1011-
candidates = []
1012-
if mapped not in candidates:
1013-
candidates.append(mapped)
1014-
base = None
1015-
try:
1016-
if "-" in mapped:
1017-
base = mapped.split("-", 1)[0]
1018-
elif "_" in mapped:
1019-
base = mapped.split("_", 1)[0]
1020-
except Exception:
1021-
base = None
1022-
if base and base not in candidates:
1023-
candidates.append(base)
1024-
if low not in candidates:
1025-
candidates.append(low)
1026-
if raw not in candidates:
1027-
candidates.append(raw)
1028-
if "en" not in candidates:
1029-
candidates.append("en")
1030-
# Load engine-local JSON using the first existing candidate
972+
from Core.engines_loader.registry import resolve_language_code
973+
974+
code = resolve_language_code(gui, tr)
975+
976+
# Load engine-local JSON using the resolved code
1031977
import importlib.resources as ilr
1032978
import json as _json
1033979

@@ -1048,6 +994,18 @@ def _load_lang(c: str) -> bool:
1048994
pass
1049995
return False
1050996

997+
# Build candidates from resolved code
998+
candidates = [code]
999+
try:
1000+
if "-" in code:
1001+
base = code.split("-", 1)[0]
1002+
if base not in candidates:
1003+
candidates.append(base)
1004+
except Exception:
1005+
pass
1006+
if "en" not in candidates:
1007+
candidates.append("en")
1008+
10511009
for cand in candidates:
10521010
if _load_lang(cand):
10531011
break

0 commit comments

Comments
 (0)