-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathlang_config.py
More file actions
313 lines (275 loc) · 11.9 KB
/
lang_config.py
File metadata and controls
313 lines (275 loc) · 11.9 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from __future__ import annotations
LANGUAGES = {
'en-GB': {
'warning_people_in_controlled_area':
'Warning: {count} people have entered the controlled area!',
'warning_no_hardhat':
'Warning: {count} people are not wearing a hardhat!',
'warning_no_safety_vest':
'Warning: {count} people are not wearing a safety vest!',
'warning_close_to_machinery':
'Warning: {count} people are too close to machinery!',
'warning_close_to_vehicle':
'Warning: {count} people are too close to vehicles!',
'warning_people_in_utility_pole_controlled_area':
'Warning: {count} people have entered the utility pole '
'restricted area!',
'detect_machinery_close_to_pole':
'Warning: {count} machineries are too close to the utility pole!',
'no_warning': 'No warning',
'machinery': 'machinery',
'vehicle': 'vehicles',
'helmet': 'helmet',
'person': 'person',
'no_helmet': 'no helmet',
'vest': 'safety vest',
'no_vest': 'no safety vest',
'mask': 'mask',
'no_mask': 'no mask',
'cone': 'cone',
'warning_notification': '[Warning Notification]',
},
'zh-TW': {
'warning_people_in_controlled_area':
'警告: 有{count}個人進入受控區域!',
'warning_no_hardhat':
'警告: 有{count}人未佩戴安全帽!',
'warning_no_safety_vest':
'警告: 有{count}人未穿著安全背心!',
'warning_close_to_machinery':
'警告: 有{count}人過於靠近機具!',
'warning_close_to_vehicle':
'警告: 有{count}人過於靠近車輛!',
'warning_people_in_utility_pole_controlled_area':
'警告: 有{count}人進入電桿管制區!',
'detect_machinery_close_to_pole':
'警告: 有{count}個機具過於靠近電桿!',
'no_warning': '無警告',
'machinery': '機具',
'vehicle': '車輛',
'helmet': '安全帽',
'person': '人員',
'no_helmet': '無安全帽',
'vest': '安全背心',
'no_vest': '無安全背心',
'mask': '口罩',
'no_mask': '無口罩',
'cone': '安全錐',
'warning_notification': '[警示通知]',
},
'zh-CN': {
'warning_people_in_controlled_area':
'警告: 有{count}个人进入受控区域!',
'warning_no_hardhat':
'警告: 有{count}人未佩戴安全帽!',
'warning_no_safety_vest':
'警告: 有{count}人未穿着安全背心!',
'warning_close_to_machinery':
'警告: 有{count}人过于靠近机械!',
'warning_close_to_vehicle':
'警告: 有{count}人过于靠近车辆!',
'warning_people_in_utility_pole_controlled_area':
'警告: 有{count}人进入电桿管制区!',
'detect_machinery_close_to_pole':
'警告: 有{count}个机械过于靠近电桿!',
'no_warning': '无警告',
'machinery': '机械',
'vehicle': '车辆',
'helmet': '安全帽',
'person': '人员',
'no_helmet': '无安全帽',
'vest': '安全背心',
'no_vest': '无安全背心',
'mask': '口罩',
'no_mask': '无口罩',
'cone': '安全锥',
'warning_notification': '[警示通知]',
},
'fr-FR': {
'warning_people_in_controlled_area':
'Avertissement: {count} personnes sont entrées dans la '
'zone contrôlée!',
'warning_no_hardhat':
'Avertissement: {count} personnes ne portent pas de casque!',
'warning_no_safety_vest':
'Avertissement: {count} personnes ne portent pas de '
'gilet de sécurité!',
'warning_close_to_machinery':
'Avertissement: Il y a {count} personnes trop proches de '
'la machinerie!',
'warning_close_to_vehicle':
'Avertissement: Il y a {count} personnes trop proches de '
'véhicules!',
'warning_people_in_utility_pole_controlled_area':
'Avertissement: {count} personnes sont entrées dans la zone '
'de poteau électrique!',
'detect_machinery_close_to_pole':
'Avertissement: {count} machines sont trop proches du poteau '
'électrique!',
'no_warning': "Pas d'avertissement",
'machinery': 'machinerie',
'vehicle': 'véhicules',
'helmet': 'casque',
'person': 'personne',
'no_helmet': 'pas de casque',
'vest': 'gilet de sécurité',
'no_vest': 'pas de gilet de sécurité',
'mask': 'masque',
'no_mask': 'pas de masque',
'cone': 'cône',
'warning_notification': '[Avertissement Notification]',
},
'vi-VN': {
'warning_people_in_controlled_area':
'Cảnh báo: Có {count} người đã vào khu vực kiểm soát!',
'warning_no_hardhat':
'Cảnh báo: Có {count} người không đội mũ bảo hộ!',
'warning_no_safety_vest':
'Cảnh báo: Có {count} người không mặc áo gi-lê an toàn!',
'warning_close_to_machinery':
'Cảnh báo: Có {count} người quá gần máy móc!',
'warning_close_to_vehicle':
'Cảnh báo: Có {count} người quá gần phương tiện!',
'warning_people_in_utility_pole_controlled_area':
'Cảnh báo: Có {count} người đã vào khu vực cột điện!',
'detect_machinery_close_to_pole':
'Cảnh báo: Có {count} máy móc quá gần cột điện!',
'no_warning': 'Không có cảnh báo',
'machinery': 'máy móc',
'vehicle': 'phương tiện',
'helmet': 'mũ bảo hộ',
'person': 'người',
'no_helmet': 'không mũ bảo hộ',
'vest': 'áo gi-lê an toàn',
'no_vest': 'không áo gi-lê an toàn',
'mask': 'khẩu trang',
'no_mask': 'không khẩu trang',
'cone': 'cọc tiêu',
'warning_notification': '[Thông báo cảnh báo]',
},
'id-ID': {
'warning_people_in_controlled_area':
'Peringatan: Ada {count} orang memasuki area terkontrol!',
'warning_no_hardhat':
'Peringatan: Ada {count} orang tidak mengenakan helm!',
'warning_no_safety_vest':
'Peringatan: Ada {count} orang tidak mengenakan '
'rompi keselamatan!',
'warning_close_to_machinery':
'Peringatan: Ada {count} orang terlalu dekat dengan mesin!',
'warning_close_to_vehicle':
'Peringatan: Ada {count} orang terlalu dekat dengan kendaraan!',
'warning_people_in_utility_pole_controlled_area':
'Peringatan: Ada {count} orang memasuki area tiang listrik!',
'detect_machinery_close_to_pole':
'Peringatan: Ada {count} mesin terlalu dekat dengan '
'tiang listrik!',
'no_warning': 'Tidak ada peringatan',
'machinery': 'mesin',
'vehicle': 'kendaraan',
'helmet': 'helm',
'person': 'orang',
'no_helmet': 'tanpa helm',
'vest': 'rompi keselamatan',
'no_vest': 'tanpa rompi keselamatan',
'mask': 'masker',
'no_mask': 'tanpa masker',
'cone': 'kerucut pengaman',
'warning_notification': '[Pemberitahuan Peringatan]',
},
'th-TH': {
'warning_people_in_controlled_area':
'คำเตือน: มี {count} คนเข้ามาในพื้นที่ควบคุม!',
'warning_no_hardhat':
'คำเตือน: มี {count} คนไม่สวมหมวกนิรภัย!',
'warning_no_safety_vest':
'คำเตือน: มี {count} คนไม่สวมเสื้อกั๊กนิรภัย!',
'warning_close_to_machinery':
'คำเตือน: มี {count} คนอยู่ใกล้เครื่องจักรมากเกินไป!',
'warning_close_to_vehicle':
'คำเตือน: มี {count} คนอยู่ใกล้ยานพาหนะมากเกินไป!',
'warning_people_in_utility_pole_controlled_area':
'คำเตือน: มี {count} คนได้เข้ามาในพื้นที่เสาไฟฟ้า!',
'detect_machinery_close_to_pole':
'คำเตือน: มี {count} เครื่องจักรอยู่ใกล้เสาไฟฟ้ามากเกินไป!',
'no_warning': 'ไม่มีคำเตือน',
'machinery': 'เครื่องจักร',
'vehicle': 'ยานพาหนะ',
'helmet': 'หมวกนิรภัย',
'person': 'บุคคล',
'no_helmet': 'ไม่สวมหมวกนิรภัย',
'vest': 'เสื้อกั๊กนิรภัย',
'no_vest': 'ไม่สวมเสื้อกั๊กนิรภัย',
'mask': 'หน้ากากอนามัย',
'no_mask': 'ไม่สวมหน้ากากอนามัย',
'cone': 'กรวยนิรภัย',
'warning_notification': '[การแจ้งเตือนความปลอดภัย]',
},
}
class Translator:
"""
A class to handle translations based on the provided language.
"""
@staticmethod
def translate_from_dict(
body_dict: dict[str, dict[str, int]],
language: str,
) -> list[str]:
"""
Translate a dictionary of warning messages into the specified language.
Example:
body_dict = {
"warning_close_to_vehicle": {"count": "3"},
"warning_no_safety_vest": {}
}
language = "zh-TW"
=> [
"警告: 有3人過於靠近車輛!",
"警告: 有人沒有穿安全背心!"
]
Args:
body_dict (dict[str, dict[str, int]]):
A dictionary where each key corresponds to a warning message
and its value is a dictionary of placeholders to be replaced.
language (str):
The locale code (e.g. "en-GB", "zh-TW") to use.
Returns:
list[str]: A list of translated warning messages.
"""
# If the language key is not in LANGUAGES, default to 'en-GB'
if language not in LANGUAGES:
language = 'en-GB'
translations: list[str] = []
lang_map = LANGUAGES[language]
for key, placeholders in body_dict.items():
template = lang_map.get(key)
if not template:
# If the key has no template, use the key itself as the message
translations.append(key)
continue
msg = template
# Replace placeholders like {count} in the template
for ph_key, ph_value in placeholders.items():
msg = msg.replace(f"{{{ph_key}}}", str(ph_value))
translations.append(msg)
return translations
def main() -> None:
"""
Simple demonstration of the Translator class's translate_from_dict method.
"""
body_dict = {
'warning_no_hardhat': {'count': 2},
'warning_no_safety_vest': {'count': 1},
'warning_people_in_utility_pole_controlled_area': {'count': 3},
}
language = 'en-GB'
translations = Translator.translate_from_dict(body_dict, language)
print(translations)
# Possible output:
# [
# "Warning: 2 people are not wearing a hardhat!",
# "Warning: 1 people are not wearing a safety vest!",
# "Warning: 3 people have entered the utility pole restricted area!"
# ]
if __name__ == '__main__':
main()