Initial commit: Robot ökoszisztéma v2.0 - Stabilizált jármű és szerviz robotok
This commit is contained in:
53
backend/app/core/i18n.py
Executable file
53
backend/app/core/i18n.py
Executable file
@@ -0,0 +1,53 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/core/i18n.py
|
||||
import json
|
||||
import os
|
||||
|
||||
class LocaleManager:
|
||||
_locales = {}
|
||||
|
||||
def get(self, key: str, lang: str = "hu", **kwargs) -> str:
|
||||
if not self._locales:
|
||||
self._load()
|
||||
|
||||
data = self._locales.get(lang, self._locales.get("hu", {}))
|
||||
# Biztonságos bejárás a pontokkal elválasztott kulcsokhoz
|
||||
for k in key.split("."):
|
||||
if isinstance(data, dict):
|
||||
data = data.get(k, {})
|
||||
else:
|
||||
return key # Ha elakadunk, adjuk vissza magát a kulcsot
|
||||
|
||||
if isinstance(data, str):
|
||||
return data.format(**kwargs)
|
||||
return key
|
||||
|
||||
def _load(self):
|
||||
# A konténeren belül ez a biztos útvonal
|
||||
possible_paths = [
|
||||
"/app/app/locales",
|
||||
"app/locales",
|
||||
"backend/app/locales"
|
||||
]
|
||||
|
||||
path = ""
|
||||
for p in possible_paths:
|
||||
if os.path.exists(p):
|
||||
path = p
|
||||
break
|
||||
|
||||
if not path:
|
||||
print("FIGYELEM: Nem található a locales könyvtár!")
|
||||
return
|
||||
|
||||
for file in os.listdir(path):
|
||||
if file.endswith(".json"):
|
||||
lang = file.split(".")[0]
|
||||
try:
|
||||
with open(os.path.join(path, file), "r", encoding="utf-8") as f:
|
||||
self._locales[lang] = json.load(f)
|
||||
except Exception as e:
|
||||
print(f"Hiba a {file} betöltésekor: {e}")
|
||||
|
||||
locale_manager = LocaleManager()
|
||||
# Rövid alias a könnyebb használathoz
|
||||
t = locale_manager.get
|
||||
Reference in New Issue
Block a user