Spaces:
Building
Building
# 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] = {} | |
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] | |
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) | |
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")] |