1-
21from __future__ import annotations
2+
33import json
44from pathlib import Path
5- from typing import Any , Dict , Optional
5+ from typing import Any
66
77class Language :
8- def __init__ (self , lang_dir : Path , code : str = "en" ) -> None :
8+ """Tiny JSON-based language lookup with dot-path keys, e.g. 'tooltips.sample_rate'."""
9+
10+ def __init__ (self , lang_dir : str | Path , code : str = "en" ) -> None :
911 self .lang_dir = Path (lang_dir )
1012 self .code = code
11- self ._data : Dict [str , Any ] = {}
13+ self ._data : dict [str , Any ] = {}
1214 self .load (self .code )
1315
1416 def load (self , code : str ) -> None :
15- self .code = code
16- path = self .lang_dir / f"{ code } .json"
1717 self ._data = {}
1818 try :
19- with open (path , "r" , encoding = "utf-8" ) as f :
19+ path = self .lang_dir / f"{ code } .json"
20+ with open (path , encoding = "utf-8" ) as f :
2021 self ._data = json .load (f )
2122 except Exception :
22- # fallback to English if available
23+ # fall back to English if not present
2324 if code != "en" :
2425 try :
25- with open (self .lang_dir / "en.json" , "r" , encoding = "utf-8" ) as f :
26+ with open (self .lang_dir / "en.json" , encoding = "utf-8" ) as f :
2627 self ._data = json .load (f )
2728 except Exception :
2829 self ._data = {}
2930
30- def get (self , key : str , default : Optional [ str ] = None ) -> Optional [ str ] :
31+ def get (self , key : str , default : str | None = None ) -> str | None :
3132 # Resolve dot path
3233 cur : Any = self ._data
3334 for part in key .split ("." ):
@@ -39,11 +40,10 @@ def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
3940 return str (cur )
4041 return default
4142
42- def available_locales (self ) -> Dict [str , Path ]:
43- locales = {}
43+ def available_locales (self ) -> dict [str , Path ]:
44+ locales : dict [ str , Path ] = {}
4445 if not self .lang_dir .exists ():
4546 return locales
4647 for p in self .lang_dir .glob ("*.json" ):
47- code = p .stem
48- locales [code ] = p
49- return dict (sorted (locales .items ()))
48+ locales [p .stem ] = p
49+ return locales
0 commit comments