mirror of
https://github.com/Redume/Shirino.git
synced 2025-07-04 03:00:55 +01:00
chore: a class is written to get and work with localization
This commit is contained in:
parent
fd6f0b699e
commit
6bf8b4c049
1 changed files with 24 additions and 0 deletions
24
i18n/localization.py
Normal file
24
i18n/localization.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import yaml
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class I18n:
|
||||||
|
"""Load every YAML file in i18n/locales and let you pull out a single-language dict."""
|
||||||
|
|
||||||
|
def __init__(self, locales_dir: str = "i18n/locales", default_lang: str = "en"):
|
||||||
|
base_path = Path(__file__).parent.parent
|
||||||
|
self.locales_dir = base_path / locales_dir
|
||||||
|
self.default_lang = default_lang
|
||||||
|
self.translations: dict[str, dict] = {}
|
||||||
|
self._load_translations()
|
||||||
|
|
||||||
|
def _load_translations(self) -> None:
|
||||||
|
for file in self.locales_dir.glob("*.yaml"):
|
||||||
|
lang = file.stem.lower()
|
||||||
|
with file.open(encoding="utf-8") as f:
|
||||||
|
self.translations[lang] = yaml.safe_load(f)
|
||||||
|
|
||||||
|
def get_locale(self, lang: str | None = None) -> dict:
|
||||||
|
"""Return the whole dictionary for one language (fallback → default_lang)."""
|
||||||
|
lang = (lang or self.default_lang).lower()[:2]
|
||||||
|
return self.translations.get(lang, self.translations[self.default_lang])
|
Loading…
Add table
Reference in a new issue