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