53 lines
1.6 KiB
Python
Executable File
53 lines
1.6 KiB
Python
Executable File
# /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 |