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", {}))
        for k in key.split("."):
            data = data.get(k, {})
        
        if isinstance(data, str):
            return data.format(**kwargs)
        return key

    def _load(self):
        path = "backend/app/locales" # Konténeren belül: "/app/app/locales"
        if not os.path.exists(path): path = "app/locales"
        
        for file in os.listdir(path):
            if file.endswith(".json"):
                lang = file.split(".")[0]
                with open(os.path.join(path, file), "r", encoding="utf-8") as f:
                    self._locales[lang] = json.load(f)

locale_manager = LocaleManager()