Coverage for opt/mealie/lib/python3.12/site-packages/mealie/pkgs/i18n/json_provider.py: 62%
43 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1import json
2from dataclasses import dataclass
3from pathlib import Path
4from typing import cast
7@dataclass(slots=True)
8class JsonProvider:
9 translations: dict
11 def __init__(self, path: Path | dict):
12 if isinstance(path, Path): 12 ↛ 15line 12 didn't jump to line 15 because the condition on line 12 was always true
13 self.translations = json.loads(path.read_text())
14 else:
15 self.translations = path
17 def _parse_plurals(self, value: str, count: float):
18 # based off of: https://kazupon.github.io/vue-i18n/guide/pluralization.html
20 values = [v.strip() for v in value.split("|")]
21 if len(values) == 1:
22 return value
23 elif len(values) == 2:
24 return values[0] if count == 1 else values[1]
25 elif len(values) == 3:
26 if count == 0:
27 return values[0]
28 else:
29 return values[1] if count == 1 else values[2]
30 else:
31 return values[0]
33 def t(self, key: str, default=None, **kwargs) -> str:
34 keys = key.split(".")
36 translation_value: dict | str = self.translations
37 last = len(keys) - 1
39 for i, k in enumerate(keys): 39 ↛ 58line 39 didn't jump to line 58 because the loop on line 39 didn't complete
40 if k not in translation_value:
41 break
43 try:
44 translation_value = translation_value[k] # type: ignore
45 except Exception:
46 break
48 if i == last:
49 for key, value in kwargs.items():
50 translation_value = cast(str, translation_value)
51 if value is None:
52 value = ""
53 if key == "count": 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true
54 translation_value = self._parse_plurals(translation_value, float(value))
55 translation_value = translation_value.replace("{" + key + "}", str(value)) # type: ignore
56 return translation_value # type: ignore
58 return default or key