MCPcopy Create free account
hub / github.com/TUM-Dev/NavigaTUM / TranslatableStr

Class TranslatableStr

data/utils.py:20–80  ·  view source on GitHub ↗

Wrapper for translations. The Wrapper takes a string, that should be translated and looks it up in the translation buffer. If the string is not found, it an entry is created in the buffer. The string can be formatted using the .format() method or left as is. Since it is a sub

Source from the content-addressed store, hash-verified

18
19
20class TranslatableStr(dict[str, str]):
21 """
22 Wrapper for translations.
23
24 The Wrapper takes a string, that should be translated and looks it up in the translation buffer.
25 If the string is not found, it an entry is created in the buffer.
26
27 The string can be formatted using the .format() method or left as is.
28
29 Since it is a subclass of dict, this class does not need any additional infrastructure
30 to turn a message into a translated string.
31
32 Translatable strings will be exported as {"de": "<de string>", "en": "<en string>"}.
33 """
34
35 def __init__(self, message: str, en_message: str | None = None) -> None:
36 if not isinstance(message, str):
37 raise TypeError("message must be a str")
38 if not message.strip():
39 raise ValueError("message must not be empty")
40 if en_message is None:
41 if message in TRANSLATION_BUFFER:
42 en_message = TRANSLATION_BUFFER[message]
43 else:
44 en_message = message
45 TRANSLATION_BUFFER[message] = ""
46 with TRANSLATION_BUFFER_PATH.open("w", encoding="utf-8") as file:
47 yaml.dump(TRANSLATION_BUFFER, file)
48 super().__init__(en=en_message, de=message)
49
50 def __hash__(self) -> int: # type: ignore[override]
51 """Return a hash as if this was a string."""
52 return hash(self["de"])
53
54 def __le__(self, other: TranslatableStr) -> bool:
55 """Compare one String to another, sorting by the german string."""
56 return str(self["de"]) <= str(other["de"])
57
58 def __lt__(self, other: TranslatableStr) -> bool:
59 """Compare one String to another, sorting by the german string."""
60 return str(self["de"]) < str(other["de"])
61
62 def __add__(self, other: str | TranslatableStr) -> TranslatableStr:
63 """Concatenate two TranslatableStr or a TranslatableStr with a string ."""
64 if isinstance(other, str):
65 return TranslatableStr(self["de"] + other, self["en"] + other)
66 if isinstance(other, TranslatableStr):
67 return TranslatableStr(self["de"] + other["de"], self["en"] + other["en"])
68 raise ValueError(f"{self} + {other} is not implmented")
69
70 def __radd__(self, other: str) -> TranslatableStr:
71 """Concatenate a TranslatableStr onto a string"""
72 if isinstance(other, str):
73 return TranslatableStr(other + self["de"], other + self["en"])
74 raise ValueError(f"{other} + {self} is not implmented")
75
76 def format(self, *args: Json, **kwargs: Json) -> TranslatableStr:
77 """Apply the format-method to the contained data, as if the class itsself was a string."""

Callers 7

_gen_computed_propsFunction · 0.90
_build_overviewFunction · 0.90
add_translatable_strFunction · 0.90
_dataFunction · 0.90
__add__Method · 0.70
__radd__Method · 0.70

Calls

no outgoing calls

Tested by 2

_dataFunction · 0.72