forked from DevilXD/TwitchDropsMiner
-
Notifications
You must be signed in to change notification settings - Fork 21
/
translate.py
277 lines (224 loc) · 5.95 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from __future__ import annotations
from collections import abc
from typing import Any, TypedDict, TYPE_CHECKING
from exceptions import MinerException
from utils import json_load
from constants import LANG_PATH, DEFAULT_LANG
import json
if TYPE_CHECKING:
from typing_extensions import NotRequired
class StatusMessages(TypedDict):
terminated: str
watching: str
goes_online: str
goes_offline: str
claimed_drop: str
claimed_points: str
earned_points: str
no_channel: str
no_campaign: str
class ChromeMessages(TypedDict):
startup: str
login_to_complete: str
no_token: str
closed_window: str
class LoginMessages(TypedDict):
chrome: ChromeMessages
error_code: str
unexpected_content: str
email_code_required: str
twofa_code_required: str
incorrect_login_pass: str
incorrect_email_code: str
incorrect_twofa_code: str
class ErrorMessages(TypedDict):
captcha: str
no_connection: str
site_down: str
class GUIStatus(TypedDict):
name: str
idle: str
exiting: str
terminated: str
cleanup: str
gathering: str
switching: str
fetching_inventory: str
fetching_campaigns: str
adding_campaigns: str
class GUITabs(TypedDict):
main: str
inventory: str
settings: str
help: str
class GUITray(TypedDict):
notification_title: str
minimize: str
show: str
quit: str
class GUILoginForm(TypedDict):
name: str
labels: str
logging_in: str
logged_in: str
logged_out: str
request: str
required: str
username: str
password: str
twofa_code: str
button: str
class GUIWebsocket(TypedDict):
name: str
websocket: str
initializing: str
connected: str
disconnected: str
connecting: str
disconnecting: str
reconnecting: str
class GUIProgress(TypedDict):
name: str
drop: str
game: str
campaign: str
remaining: str
drop_progress: str
campaign_progress: str
class GUIChannelHeadings(TypedDict):
channel: str
status: str
game: str
points: str
viewers: str
class GUIChannels(TypedDict):
name: str
switch: str
load_points: str
online: str
pending: str
offline: str
headings: GUIChannelHeadings
class GUIInvFilter(TypedDict):
name: str
show: str
not_linked: str
upcoming: str
expired: str
excluded: str
finished: str
refresh: str
class GUIInvStatus(TypedDict):
linked: str
not_linked: str
active: str
expired: str
upcoming: str
claimed: str
ready_to_claim: str
class GUIInventory(TypedDict):
filter: GUIInvFilter
status: GUIInvStatus
starts: str
ends: str
allowed_channels: str
all_channels: str
and_more: str
percent_progress: str
minutes_progress: str
class GUISettingsGeneral(TypedDict):
name: str
autostart: str
tray: str
tray_notifications: str
priority_only: str
prioritize_by_ending_soonest: str
proxy: str
dark_theme: str
class GUISettings(TypedDict):
general: GUISettingsGeneral
game_name: str
priority: str
exclude: str
reload: str
reload_text: str
class GUIHelpLinks(TypedDict):
name: str
inventory: str
campaigns: str
class GUIHelp(TypedDict):
links: GUIHelpLinks
how_it_works: str
how_it_works_text: str
getting_started: str
getting_started_text: str
class GUIMessages(TypedDict):
output: str
status: GUIStatus
tabs: GUITabs
tray: GUITray
login: GUILoginForm
websocket: GUIWebsocket
progress: GUIProgress
channels: GUIChannels
inventory: GUIInventory
settings: GUISettings
help: GUIHelp
class Translation(TypedDict):
language_name: NotRequired[str]
english_name: str
status: StatusMessages
login: LoginMessages
error: ErrorMessages
gui: GUIMessages
with open(LANG_PATH.joinpath(f"{DEFAULT_LANG}.json"), 'r', encoding='utf-8') as file:
default_translation: Translation = json.load(file)
class Translator:
def __init__(self) -> None:
self._langs: list[str] = []
# start with (and always copy) the default translation
self._translation: Translation = default_translation.copy()
self._translation["language_name"] = DEFAULT_LANG
# load available translation names
for filepath in LANG_PATH.glob("*.json"):
self._langs.append(filepath.stem)
self._langs.sort()
if DEFAULT_LANG in self._langs:
self._langs.remove(DEFAULT_LANG)
self._langs.insert(0, DEFAULT_LANG)
@property
def languages(self) -> abc.Iterable[str]:
return iter(self._langs)
@property
def current(self) -> str:
return self._translation["language_name"]
def set_language(self, language: str):
if language not in self._langs:
raise ValueError("Unrecognized language")
elif self._translation["language_name"] == language:
# same language as loaded selected
return
elif language == DEFAULT_LANG:
# default language selected - use the memory value
self._translation = default_translation.copy()
else:
self._translation = json_load(
LANG_PATH.joinpath(f"{language}.json"), default_translation
)
if "language_name" in self._translation:
raise ValueError("Translations cannot define 'language_name'")
self._translation["language_name"] = language
def __call__(self, *path: str) -> str:
if not path:
raise ValueError("Language path expected")
v: Any = self._translation
try:
for key in path:
v = v[key]
except KeyError:
# this can only really happen for the default translation
raise MinerException(
f"{self.current} translation is missing the '{' -> '.join(path)}' translation key"
)
return v
_ = Translator()