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-12-05 13:45 +0000

1import json 1o

2from dataclasses import dataclass 1o

3from pathlib import Path 1o

4from typing import cast 1o

5 

6 

7@dataclass(slots=True) 1o

8class JsonProvider: 1o

9 translations: dict 1o

10 

11 def __init__(self, path: Path | dict): 1o

12 if isinstance(path, Path): 12 ↛ 15line 12 didn't jump to line 15 because the condition on line 12 was always true1plqmabcdefghijk

13 self.translations = json.loads(path.read_text()) 1plqmabcdefghijk

14 else: 

15 self.translations = path 

16 

17 def _parse_plurals(self, value: str, count: float): 1o

18 # based off of: https://kazupon.github.io/vue-i18n/guide/pluralization.html 

19 

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] 

32 

33 def t(self, key: str, default=None, **kwargs) -> str: 1o

34 keys = key.split(".") 1lmabcdefghijnk

35 

36 translation_value: dict | str = self.translations 1lmabcdefghijnk

37 last = len(keys) - 1 1lmabcdefghijnk

38 

39 for i, k in enumerate(keys): 39 ↛ 58line 39 didn't jump to line 58 because the loop on line 39 didn't complete1lmabcdefghijnk

40 if k not in translation_value: 1lmabcdefghijnk

41 break 1lmabcdefghijk

42 

43 try: 1lmabcdefghijnk

44 translation_value = translation_value[k] # type: ignore 1lmabcdefghijnk

45 except Exception: 

46 break 

47 

48 if i == last: 1lmabcdefghijnk

49 for key, value in kwargs.items(): 1lmabcdefghijnk

50 translation_value = cast(str, translation_value) 1labcdefghijnk

51 if value is None: 1labcdefghijnk

52 value = "" 1abcdefghijk

53 if key == "count": 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true1labcdefghijnk

54 translation_value = self._parse_plurals(translation_value, float(value)) 

55 translation_value = translation_value.replace("{" + key + "}", str(value)) # type: ignore 1labcdefghijnk

56 return translation_value # type: ignore 1lmabcdefghijnk

57 

58 return default or key 1lmabcdefghijk