Spaces:
Sleeping
Sleeping
Create utils_lang.py
Browse files- utils_lang.py +74 -0
utils_lang.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
utils_lang.py
|
3 |
+
Step 5: Language helpers for EN / FR / Kreol Morisien (MFE).
|
4 |
+
|
5 |
+
(Objective)
|
6 |
+
- We provide a tiny, dependency-free way to localize the UI labels and
|
7 |
+
fixed phrases. Content retrieved from your docs stays as-is; we only
|
8 |
+
translate the "glue text" we control.
|
9 |
+
|
10 |
+
- You can add more phrases later in one place.
|
11 |
+
"""
|
12 |
+
|
13 |
+
from typing import Dict
|
14 |
+
|
15 |
+
# Supported language codes and display names (Objective)
|
16 |
+
SUPPORTED = {
|
17 |
+
"en": "English",
|
18 |
+
"fr": "Français",
|
19 |
+
"mfe": "Kreol Morisien",
|
20 |
+
}
|
21 |
+
|
22 |
+
def normalize_lang(code: str) -> str:
|
23 |
+
"""(Objective) Ensure a valid code; default to English."""
|
24 |
+
return code if code in SUPPORTED else "en"
|
25 |
+
|
26 |
+
|
27 |
+
# Minimal localized strings used by the app (Objective)
|
28 |
+
_STRINGS: Dict[str, Dict[str, str]] = {
|
29 |
+
"en": {
|
30 |
+
"title": "Evo Government Copilot (Mauritius)",
|
31 |
+
"ask_placeholder": "e.g., How do I renew my Mauritius passport?",
|
32 |
+
"ask_label": "Your question",
|
33 |
+
"k_label": "Context depth (top-k)",
|
34 |
+
"build_btn": "🔧 Build/Refresh Index (TXT)",
|
35 |
+
"status_idle": "Status: _idle_",
|
36 |
+
"intro_ok": "Answer (grounded in retrieved sources):",
|
37 |
+
"intro_err": "I couldn’t find enough information. Try rephrasing or add official documents.",
|
38 |
+
"sources": "Sources",
|
39 |
+
"tip_build": "Tip: Click 'Build/Refresh Index' first.",
|
40 |
+
},
|
41 |
+
"fr": {
|
42 |
+
"title": "Copilote Gouvernemental Evo (Maurice)",
|
43 |
+
"ask_placeholder": "ex. Comment puis-je renouveler mon passeport mauricien ?",
|
44 |
+
"ask_label": "Votre question",
|
45 |
+
"k_label": "Profondeur du contexte (top-k)",
|
46 |
+
"build_btn": "🔧 Construire/Actualiser l’index (TXT)",
|
47 |
+
"status_idle": "Statut : _inactif_",
|
48 |
+
"intro_ok": "Réponse (fondée sur les sources récupérées) :",
|
49 |
+
"intro_err": "Je n’ai pas trouvé assez d’informations. Reformulez ou ajoutez des documents officiels.",
|
50 |
+
"sources": "Sources",
|
51 |
+
"tip_build": "Astuce : cliquez d’abord sur « Construire/Actualiser l’index ».",
|
52 |
+
},
|
53 |
+
"mfe": {
|
54 |
+
"title": "Evo Gouv Copilot (Morisi)",
|
55 |
+
"ask_placeholder": "eg. Kouma mo kapav renouvle mo paspor Morisien?",
|
56 |
+
"ask_label": "To kestyon",
|
57 |
+
"k_label": "Profonder konteks (top-k)",
|
58 |
+
"build_btn": "🔧 Bati/Rafresir Index (TXT)",
|
59 |
+
"status_idle": "Sta: _repoze_",
|
60 |
+
"intro_ok": "Repons (lor sours ki finn gagne):",
|
61 |
+
"intro_err": "Pena ase informasion. Esay rephrase ouswa azout dokiman ofisiel.",
|
62 |
+
"sources": "Sours",
|
63 |
+
"tip_build": "Tip: Klik « Bati/Rafresir Index » avan.",
|
64 |
+
},
|
65 |
+
}
|
66 |
+
|
67 |
+
def L(lang: str, key: str) -> str:
|
68 |
+
"""
|
69 |
+
(Objective) Return localized string; fallback to English if missing.
|
70 |
+
"""
|
71 |
+
lang = normalize_lang(lang)
|
72 |
+
if key in _STRINGS.get(lang, {}):
|
73 |
+
return _STRINGS[lang][key]
|
74 |
+
return _STRINGS["en"].get(key, key)
|