File size: 1,497 Bytes
80743f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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")]