Spaces:
				
			
			
	
			
			
		Paused
		
	
	
	
			
			
	
	
	
	
		
		
		Paused
		
	Update locale_manager.py
Browse files- locale_manager.py +116 -5
 
    	
        locale_manager.py
    CHANGED
    
    | 
         @@ -1,12 +1,24 @@ 
     | 
|
| 1 | 
         
             
            # locale_manager.py
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 2 | 
         
             
            import json
         
     | 
| 3 | 
         
             
            from pathlib import Path
         
     | 
| 4 | 
         
            -
            from typing import Dict, Optional
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 5 | 
         | 
| 6 | 
         
             
            class LocaleManager:
         
     | 
| 7 | 
         
            -
                """Manages locale files for TTS preprocessing"""
         
     | 
| 8 | 
         | 
| 9 | 
         
             
                _cache: Dict[str, Dict] = {}
         
     | 
| 
         | 
|
| 10 | 
         | 
| 11 | 
         
             
                @classmethod
         
     | 
| 12 | 
         
             
                def get_locale(cls, language: str) -> Dict:
         
     | 
| 
         @@ -33,11 +45,110 @@ class LocaleManager: 
     | 
|
| 33 | 
         
             
                    else:
         
     | 
| 34 | 
         
             
                        # Return English as fallback
         
     | 
| 35 | 
         
             
                        fallback_file = base_path / "en.json"
         
     | 
| 36 | 
         
            -
                         
     | 
| 37 | 
         
            -
                             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 38 | 
         | 
| 39 | 
         
             
                @classmethod
         
     | 
| 40 | 
         
             
                def list_available_locales(cls) -> List[str]:
         
     | 
| 41 | 
         
             
                    """List all available locale files"""
         
     | 
| 42 | 
         
             
                    base_path = Path(__file__).parent / "locales"
         
     | 
| 43 | 
         
            -
                     
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
             
            # locale_manager.py
         
     | 
| 2 | 
         
            +
            """
         
     | 
| 3 | 
         
            +
            Locale Manager - Manages locale files for the system
         
     | 
| 4 | 
         
            +
            """
         
     | 
| 5 | 
         
            +
             
     | 
| 6 | 
         
             
            import json
         
     | 
| 7 | 
         
             
            from pathlib import Path
         
     | 
| 8 | 
         
            +
            from typing import Dict, List, Optional
         
     | 
| 9 | 
         
            +
            from datetime import datetime
         
     | 
| 10 | 
         
            +
            import sys
         
     | 
| 11 | 
         
            +
             
     | 
| 12 | 
         
            +
            def log(message: str):
         
     | 
| 13 | 
         
            +
                timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
         
     | 
| 14 | 
         
            +
                print(f"[{timestamp}] {message}")
         
     | 
| 15 | 
         
            +
                sys.stdout.flush()
         
     | 
| 16 | 
         | 
| 17 | 
         
             
            class LocaleManager:
         
     | 
| 18 | 
         
            +
                """Manages locale files for TTS preprocessing and system-wide language support"""
         
     | 
| 19 | 
         | 
| 20 | 
         
             
                _cache: Dict[str, Dict] = {}
         
     | 
| 21 | 
         
            +
                _available_locales: Optional[List[Dict[str, str]]] = None
         
     | 
| 22 | 
         | 
| 23 | 
         
             
                @classmethod
         
     | 
| 24 | 
         
             
                def get_locale(cls, language: str) -> Dict:
         
     | 
| 
         | 
|
| 45 | 
         
             
                    else:
         
     | 
| 46 | 
         
             
                        # Return English as fallback
         
     | 
| 47 | 
         
             
                        fallback_file = base_path / "en.json"
         
     | 
| 48 | 
         
            +
                        if fallback_file.exists():
         
     | 
| 49 | 
         
            +
                            with open(fallback_file, 'r', encoding='utf-8') as f:
         
     | 
| 50 | 
         
            +
                                return json.load(f)
         
     | 
| 51 | 
         
            +
                        else:
         
     | 
| 52 | 
         
            +
                            # Minimal fallback if no locale files exist
         
     | 
| 53 | 
         
            +
                            return {
         
     | 
| 54 | 
         
            +
                                "code": "en",
         
     | 
| 55 | 
         
            +
                                "name": "English",
         
     | 
| 56 | 
         
            +
                                "english_name": "English"
         
     | 
| 57 | 
         
            +
                            }
         
     | 
| 58 | 
         | 
| 59 | 
         
             
                @classmethod
         
     | 
| 60 | 
         
             
                def list_available_locales(cls) -> List[str]:
         
     | 
| 61 | 
         
             
                    """List all available locale files"""
         
     | 
| 62 | 
         
             
                    base_path = Path(__file__).parent / "locales"
         
     | 
| 63 | 
         
            +
                    if not base_path.exists():
         
     | 
| 64 | 
         
            +
                        return ["en", "tr"]  # Default locales
         
     | 
| 65 | 
         
            +
                    return [f.stem for f in base_path.glob("*.json")]
         
     | 
| 66 | 
         
            +
                
         
     | 
| 67 | 
         
            +
                @classmethod
         
     | 
| 68 | 
         
            +
                def get_available_locales_with_names(cls) -> List[Dict[str, str]]:
         
     | 
| 69 | 
         
            +
                    """Get list of all available locales with their display names"""
         
     | 
| 70 | 
         
            +
                    if cls._available_locales is not None:
         
     | 
| 71 | 
         
            +
                        return cls._available_locales
         
     | 
| 72 | 
         
            +
                    
         
     | 
| 73 | 
         
            +
                    cls._available_locales = []
         
     | 
| 74 | 
         
            +
                    base_path = Path(__file__).parent / "locales"
         
     | 
| 75 | 
         
            +
                    
         
     | 
| 76 | 
         
            +
                    if not base_path.exists():
         
     | 
| 77 | 
         
            +
                        # Return default locales if directory doesn't exist
         
     | 
| 78 | 
         
            +
                        cls._available_locales = [
         
     | 
| 79 | 
         
            +
                            {
         
     | 
| 80 | 
         
            +
                                "code": "tr-TR",
         
     | 
| 81 | 
         
            +
                                "name": "Türkçe",
         
     | 
| 82 | 
         
            +
                                "english_name": "Turkish"
         
     | 
| 83 | 
         
            +
                            },
         
     | 
| 84 | 
         
            +
                            {
         
     | 
| 85 | 
         
            +
                                "code": "en-US",
         
     | 
| 86 | 
         
            +
                                "name": "English",
         
     | 
| 87 | 
         
            +
                                "english_name": "English (US)"
         
     | 
| 88 | 
         
            +
                            }
         
     | 
| 89 | 
         
            +
                        ]
         
     | 
| 90 | 
         
            +
                        return cls._available_locales
         
     | 
| 91 | 
         
            +
                    
         
     | 
| 92 | 
         
            +
                    # Load all locale files
         
     | 
| 93 | 
         
            +
                    for locale_file in base_path.glob("*.json"):
         
     | 
| 94 | 
         
            +
                        try:
         
     | 
| 95 | 
         
            +
                            locale_code = locale_file.stem
         
     | 
| 96 | 
         
            +
                            locale_data = cls.get_locale(locale_code)
         
     | 
| 97 | 
         
            +
                            
         
     | 
| 98 | 
         
            +
                            cls._available_locales.append({
         
     | 
| 99 | 
         
            +
                                "code": locale_code,
         
     | 
| 100 | 
         
            +
                                "name": locale_data.get("name", locale_code),
         
     | 
| 101 | 
         
            +
                                "english_name": locale_data.get("english_name", locale_code)
         
     | 
| 102 | 
         
            +
                            })
         
     | 
| 103 | 
         
            +
                            
         
     | 
| 104 | 
         
            +
                            log(f"✅ Loaded locale: {locale_code} - {locale_data.get('name', 'Unknown')}")
         
     | 
| 105 | 
         
            +
                            
         
     | 
| 106 | 
         
            +
                        except Exception as e:
         
     | 
| 107 | 
         
            +
                            log(f"❌ Failed to load locale {locale_file}: {e}")
         
     | 
| 108 | 
         
            +
                    
         
     | 
| 109 | 
         
            +
                    # Sort by name for consistent ordering
         
     | 
| 110 | 
         
            +
                    cls._available_locales.sort(key=lambda x: x['name'])
         
     | 
| 111 | 
         
            +
                    
         
     | 
| 112 | 
         
            +
                    return cls._available_locales
         
     | 
| 113 | 
         
            +
                
         
     | 
| 114 | 
         
            +
                @classmethod
         
     | 
| 115 | 
         
            +
                def get_locale_details(cls, locale_code: str) -> Optional[Dict]:
         
     | 
| 116 | 
         
            +
                    """Get detailed info for a specific locale"""
         
     | 
| 117 | 
         
            +
                    try:
         
     | 
| 118 | 
         
            +
                        locale_data = cls.get_locale(locale_code)
         
     | 
| 119 | 
         
            +
                        # Ensure code is in the data
         
     | 
| 120 | 
         
            +
                        locale_data['code'] = locale_code
         
     | 
| 121 | 
         
            +
                        return locale_data
         
     | 
| 122 | 
         
            +
                    except:
         
     | 
| 123 | 
         
            +
                        return None
         
     | 
| 124 | 
         
            +
                
         
     | 
| 125 | 
         
            +
                @classmethod
         
     | 
| 126 | 
         
            +
                def is_locale_supported(cls, locale_code: str) -> bool:
         
     | 
| 127 | 
         
            +
                    """Check if a locale is supported system-wide"""
         
     | 
| 128 | 
         
            +
                    available_codes = [locale['code'] for locale in cls.get_available_locales_with_names()]
         
     | 
| 129 | 
         
            +
                    return locale_code in available_codes
         
     | 
| 130 | 
         
            +
                
         
     | 
| 131 | 
         
            +
                @classmethod
         
     | 
| 132 | 
         
            +
                def validate_project_languages(cls, languages: List[str]) -> List[str]:
         
     | 
| 133 | 
         
            +
                    """Validate that all languages are system-supported, return invalid ones"""
         
     | 
| 134 | 
         
            +
                    available_codes = [locale['code'] for locale in cls.get_available_locales_with_names()]
         
     | 
| 135 | 
         
            +
                    invalid_languages = [
         
     | 
| 136 | 
         
            +
                        lang for lang in languages 
         
     | 
| 137 | 
         
            +
                        if lang not in available_codes
         
     | 
| 138 | 
         
            +
                    ]
         
     | 
| 139 | 
         
            +
                    return invalid_languages
         
     | 
| 140 | 
         
            +
                
         
     | 
| 141 | 
         
            +
                @classmethod
         
     | 
| 142 | 
         
            +
                def get_default_locale(cls) -> str:
         
     | 
| 143 | 
         
            +
                    """Get system default locale"""
         
     | 
| 144 | 
         
            +
                    available_locales = cls.get_available_locales_with_names()
         
     | 
| 145 | 
         
            +
                    
         
     | 
| 146 | 
         
            +
                    # Priority: tr-TR, en-US, first available
         
     | 
| 147 | 
         
            +
                    for preferred in ["tr-TR", "en-US"]:
         
     | 
| 148 | 
         
            +
                        if any(locale['code'] == preferred for locale in available_locales):
         
     | 
| 149 | 
         
            +
                            return preferred
         
     | 
| 150 | 
         
            +
                    
         
     | 
| 151 | 
         
            +
                    # Return first available or fallback
         
     | 
| 152 | 
         
            +
                    if available_locales:
         
     | 
| 153 | 
         
            +
                        return available_locales[0]['code']
         
     | 
| 154 | 
         
            +
                    return "en-US"
         
     |