Skip to content

Commit 721edde

Browse files
authored
Update i18n.py
1 parent 8eae363 commit 721edde

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

i18n.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
21
from __future__ import annotations
2+
33
import json
44
from pathlib import Path
5-
from typing import Any, Dict, Optional
5+
from typing import Any
66

77
class 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

Comments
 (0)