flare / locale_manager.py
ciyidogan's picture
Create locale_manager.py
80743f7 verified
raw
history blame
1.5 kB
# locale_manager.py
import json
from pathlib import Path
from typing import Dict, Optional
class LocaleManager:
"""Manages locale files for TTS preprocessing"""
_cache: Dict[str, Dict] = {}
@classmethod
def get_locale(cls, language: str) -> Dict:
"""Get locale data with caching"""
if language not in cls._cache:
cls._cache[language] = cls._load_locale(language)
return cls._cache[language]
@classmethod
def _load_locale(cls, language: str) -> Dict:
"""Load locale from file"""
base_path = Path(__file__).parent / "locales"
locale_file = base_path / f"{language}.json"
if not locale_file.exists():
# Try language code without region (tr-TR -> tr)
if '-' in language:
language = language.split('-')[0]
locale_file = base_path / f"{language}.json"
if locale_file.exists():
with open(locale_file, 'r', encoding='utf-8') as f:
return json.load(f)
else:
# Return English as fallback
fallback_file = base_path / "en.json"
with open(fallback_file, 'r', encoding='utf-8') as f:
return json.load(f)
@classmethod
def list_available_locales(cls) -> List[str]:
"""List all available locale files"""
base_path = Path(__file__).parent / "locales"
return [f.stem for f in base_path.glob("*.json")]