Coverage for opt/mealie/lib/python3.12/site-packages/mealie/pkgs/i18n/json_provider.py: 62%
43 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
1import json 1k
2from dataclasses import dataclass 1k
3from pathlib import Path 1k
4from typing import cast 1k
7@dataclass(slots=True) 1k
8class JsonProvider: 1k
9 translations: dict 1k
11 def __init__(self, path: Path | dict): 1k
12 if isinstance(path, Path): 12 ↛ 15line 12 didn't jump to line 15 because the condition on line 12 was always true1lmnabcdefghij
13 self.translations = json.loads(path.read_text()) 1lmnabcdefghij
14 else:
15 self.translations = path
17 def _parse_plurals(self, value: str, count: float): 1k
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: 1k
34 keys = key.split(".") 1abcdefghij
36 translation_value: dict | str = self.translations 1abcdefghij
37 last = len(keys) - 1 1abcdefghij
39 for i, k in enumerate(keys): 39 ↛ 58line 39 didn't jump to line 58 because the loop on line 39 didn't complete1abcdefghij
40 if k not in translation_value: 1abcdefghij
41 break 1abcdefghij
43 try: 1abcdefghij
44 translation_value = translation_value[k] # type: ignore 1abcdefghij
45 except Exception:
46 break
48 if i == last: 1abcdefghij
49 for key, value in kwargs.items(): 1abcdefghij
50 translation_value = cast(str, translation_value) 1abcdefghij
51 if value is None: 1abcdefghij
52 value = "" 1abcdefghij
53 if key == "count": 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true1abcdefghij
54 translation_value = self._parse_plurals(translation_value, float(value))
55 translation_value = translation_value.replace("{" + key + "}", str(value)) # type: ignore 1abcdefghij
56 return translation_value # type: ignore 1abcdefghij
58 return default or key 1abcdefghij