Spaces:
Running
Running
Create locale_manager.py
Browse files- locale_manager.py +43 -0
locale_manager.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
13 |
+
"""Get locale data with caching"""
|
14 |
+
if language not in cls._cache:
|
15 |
+
cls._cache[language] = cls._load_locale(language)
|
16 |
+
return cls._cache[language]
|
17 |
+
|
18 |
+
@classmethod
|
19 |
+
def _load_locale(cls, language: str) -> Dict:
|
20 |
+
"""Load locale from file"""
|
21 |
+
base_path = Path(__file__).parent / "locales"
|
22 |
+
locale_file = base_path / f"{language}.json"
|
23 |
+
|
24 |
+
if not locale_file.exists():
|
25 |
+
# Try language code without region (tr-TR -> tr)
|
26 |
+
if '-' in language:
|
27 |
+
language = language.split('-')[0]
|
28 |
+
locale_file = base_path / f"{language}.json"
|
29 |
+
|
30 |
+
if locale_file.exists():
|
31 |
+
with open(locale_file, 'r', encoding='utf-8') as f:
|
32 |
+
return json.load(f)
|
33 |
+
else:
|
34 |
+
# Return English as fallback
|
35 |
+
fallback_file = base_path / "en.json"
|
36 |
+
with open(fallback_file, 'r', encoding='utf-8') as f:
|
37 |
+
return json.load(f)
|
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 |
+
return [f.stem for f in base_path.glob("*.json")]
|