-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathi18n.py
More file actions
411 lines (358 loc) · 18.3 KB
/
i18n.py
File metadata and controls
411 lines (358 loc) · 18.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from contextlib import contextmanager
from dataclasses import dataclass
from typing import *
from . import lang
from . import location
from . import utils
type Lang = Literal['en', 'de']
allLanguages: list[Lang] = ['en', 'de']
# If not set explicitly, the current locale is asked for the language
_lang: Optional[Lang] = None
@contextmanager
def explicitLang(newLang: Lang):
"""Context manager to temporarily set the language."""
global _lang
oldLang = _lang
_lang = newLang
try:
yield
finally:
_lang = oldLang
def getLang() -> Lang:
if _lang:
return _lang
else:
return lang.pickLanguage(allLanguages, 'en')
def setLang(lang: Lang):
global _lang
_lang = lang
def tr(key: str, **kws) -> str:
match getLang():
case 'en':
tmpl = key
case 'de':
if key not in DE and utils.isUnderTest():
raise ValueError(f'Untranslated string: {key}')
tmpl = DE.get(key, key)
case x:
if utils.underTest():
raise ValueError(f'Unknown language: {x}')
return tmpl.format(**kws)
DE = {
'Expecting no return value when calling function `{fun}`.':
'Kein Rückgabewert erwartet bei Aufruf der Funktion `{fun}`.',
'Expecting no return value when calling method `{method}` of class `{cls}`.':
'Kein Rückgabewert erwartet bei Aufruf der Methode `{method}` der Klasse `{cls}`.',
'Expecting no return value when calling constructor of record `{cls}`.':
'Kein Rückgabewert erwartet bei Aufruf des Konstruktors des Records `{cls}`.',
'Expecting return value of type `{ty}` when calling function `{fun}`.':
'Rückgabewert vom Typ `{ty}` erwartet bei Aufruf der Funktion `{fun}`.',
'Expecting return value of type `{ty}` when calling method `{method}` of class `{cls}`.':
'Rückgabewert vom Typ `{ty}` erwartet bei Aufruf der Methode `{method}` der Klasse `{cls}`.',
'Expecting return value of type `{ty}` when calling constructor of record `{cls}`.':
'Rückgabewert vom Typ `{ty}` erwartet bei Aufruf des Konstruktors des Records `{cls}`.',
'But the call returns a value of type `{ty}`.':
'Aber der Aufruf gibt einen Wert vom Typ `{ty}` zurück.',
'But no value returned.':
'Aber kein Rückgabewert vorhanden.',
'no result returned': 'kein Rückgabewert vorhanden',
'Call in line {line} causes the function to return no value:':
'Aufruf in Zeile {line} führt dazu, dass die Funktion keinen Wert zurückgibt:',
'The call of function `{fun}` expects value of type `{ty}` as {arg}.':
'Der Aufruf der Funktion `{fun}` erwartet einen Wert vom Typ `{ty}` als {arg}.',
'The call of method `{method}` of class `{cls}` expects value of type `{ty}` as {arg}.':
'Der Aufruf der Methode `{method}` der Klasse `{cls}` erwartet einen Wert vom Typ `{ty}` als {arg}.',
'The call of the constructor of record `{cls}` expects value of type `{ty}` as {arg}.':
'Der Aufruf des Konstruktors des Records `{cls}` erwartet einen Wert vom Typ `{ty}` als {arg}.',
'But the value given has type `{ty}`.': 'Aber der übergebene Wert hat den Typ `{ty}`.',
'But the default value has type `{ty}`.': 'Aber der Default-Wert hat den Typ `{ty}`.',
'File': 'Datei',
'Line': 'Zeile',
'Result type declared in line': 'Rückgabetyp deklariert in Zeile',
'Type declared in line': 'Typ deklariert in Zeile',
'Parameter declared in line': 'Parameter deklariert in Zeile',
'Problematic return in line': 'Fehlerhaftes return in Zeile',
'Problematic call in line': 'Fehlerhafter Aufruf in Zeile',
'Call in line {line} causes the problematic return:':
'Aufruf in Zeile {line} verursacht das fehlerhafte return:',
'Parameter `{param}` of function `{fun}` requires a type annotation.':
'Parameter `{param}` der Funktion `{fun}` benötigt eine Typannotation.',
'Parameter `{param}` of method `{method}` from class `{cls}` requires a type annotation.':
'Parameter `{param}` der Methode `{method}` der Klasse `{cls}` benötigt eine Typannotation.',
'Parameter `{param}` of constructor of record `{cls}` requires a type annotation.':
'Parameter `{param}` des Konstruktors des Records `{cls}` benötigt eine Typannotation.',
'Attribute `{name}` of record `{record}` requires a type annotation.':
'Attribut `{name}` des Records `{record}` benötigt eine Typannotation.',
'invalid type `{ty}`':
'ungültiger Typ `{ty}`',
'invalid type for rest argument: `{ty}`':
'ungültiger Typ für Restargument: `{ty}`',
'invalid type for keyword argument: `{ty}`':
'ungültiger Typ für Schlüsselwort-Argument: `{ty}`',
'Cannot set attribute to value of type `{ty}`.':
'Das Attribut kann nicht auf einen Wert vom Typ `{ty}` gesetzt werden.',
'Problematic assignment in line': 'Fehlerhafte Zuweisung in Zeile',
'Attribute `{attrName}` of record `{recordName}` declared with type `{ty}`.':
'Attribut `{attrName}` des Records `{recordName}` deklariert als Typ `{ty}`.',
'argument count mismatch': 'Anzahl der Argumente stimmt nicht',
'Call in line': 'Aufruf in Zeile',
'Function `{fun}` takes ': 'Funktion `{fun}` benötigt ',
'Function `{fun}` takes at least ': 'Funktion `{fun}` benötigt mindestens ',
'Function `{fun}` takes at most ': 'Funktion `{fun}` akzeptiert höchstens ',
'Method `{method}` of class `{cls}` takes ': 'Methode `{method}` der Klasse `{cls}` benötigt ',
'Method `{method}` of class `{cls}` takes at least ': 'Methode `{method}` der Klasse `{cls}` benötigt mindestens ',
'Method `{method}` of class `{cls}` takes at most ': 'Methode `{method}` der Klasse `{cls}` akzeptiert höchstens ',
'Constructor of record `{cls}` takes ': 'Konstruktor des Records `{cls}` benötigt ',
'Constructor of record `{cls}` takes at least ': 'Konstruktor des Records `{cls}` benötigt mindestens ',
'Constructor of record `{cls}` takes at most ': 'Konstruktor des Records `{cls}` akzeptiert höchstens ',
'Given: ': 'Gegeben: ',
'Default value for parameter `{paramName}` of function `{fun}` must have type `{ty}`.':
'Default-Wert des Parameters `{paramName}` der Funktion `{fun}` muss vom Typ `{ty}` sein.',
'Default value for parameter `{paramName}` of method `{method}` in class `{cls}` must have type `{ty}`.':
'Default-Wert des Parameters `{paramName}` der Methode `{method}` der Klasse `{cls}` muss vom Typ `{ty}` sein.',
'Default value for attribute `{paramName}` of record `{cls}` must have type `{ty}`.':
'Default-Wert des Attributs `{paramName}` des Records `{cls}` muss vom Typ `{ty}` sein.',
'Unknown attribute {attrName} for record {clsName}':
'Attribut {attrName} ist nicht bekannt für den Record {clsName}',
'Did you mean `{ty}`?': 'Wolltest du `{ty}` schreiben?',
'unknown keyword argument': 'unbekanntes Schlüsselwort-Argument',
'Function `{fun}` does not accept keyword argument `{name}`.':
'Funktion `{fun}` akzeptiert kein Schlüsselwort-Argument `{name}`.',
'Method `{method}` from class `{cls}` does not accept keyword argument `{name}`.':
'Methode `{method}` der Klasse `{cls}` akzeptiert kein Schlüsselwort-Argument `{name}`.',
'Constructor of record `{cls}` does not accept keyword argument `{name}`.':
'Konstruktor des Records `{cls}` akzeptiert kein Schlüsselwort-Argument `{name}`.',
'invalid record definition': 'ungültige Record-Definition',
'Expected {expected}, but the result is {actual}':
'Erwartet wird {expected}, aber das Ergebnis ist {actual}',
'ERROR: ': 'FEHLER: ',
'ERROR in ': 'FEHLER in ',
'File {filename}, line {lineno}: ': 'Datei {filename}, Zeile {lineno}: ',
'Uncovered case': 'Ein Fall ist nicht abgedeckt',
'uncovered case': 'ein Fall ist nicht abgedeckt',
'The impossible happened!': 'Das Unmögliche ist passiert!',
'Stop of execution': 'Abbruch der Ausführung',
'1 successful test': '1 erfolgreicher Test',
'all successful': 'alle erfolgreich',
'and stop of execution': 'und Abbruch der Ausführung',
'=== WELCOME to ': '=== WILLKOMMEN bei '
}
def expectingNoReturn(cn: location.CallableName) -> str:
match cn.kind:
case 'function':
return tr('Expecting no return value when calling function `{fun}`.',
fun=cn.name)
case location.ClassMember('method', cls):
return tr('Expecting no return value when calling method `{method}` of class `{cls}`.',
method=cn.name, cls=cls)
case location.ClassMember('recordConstructor', cls):
return tr('Expecting no return value when calling constructor of record `{cls}`.',
cls=cls)
raise ValueError(f'Unexpected: {cn}')
def wrongReturnValue(ty: str) -> str:
return tr('But the call returns a value of type `{ty}`.', ty=ty)
def unexpectedReturn(line: int) -> str:
return tr('Call in line {line} causes the problematic return:', line=line)
def unexpectedNoReturn(line: int) -> str:
return tr('Call in line {line} causes the function to return no value:', line=line)
def expectingReturnOfType(cn: location.CallableName, ty: str) -> str:
match cn.kind:
case 'function':
return tr('Expecting return value of type `{ty}` when calling function `{fun}`.',
fun=cn.name, ty=ty)
case location.ClassMember('method', cls):
return tr('Expecting return value of type `{ty}` when calling method `{method}` of class `{cls}`.',
method=cn.name, cls=cls, ty=ty)
case location.ClassMember('recordConstructor', cls):
return tr('Expecting return value of type `{ty}` when calling constructor of record `{cls}`.',
cls=cls, ty=ty)
raise ValueError(f'Unexpected: {cn}')
def noReturnValue() -> str:
return tr('But no value returned.')
def transArg(pos: int):
match getLang():
case 'en':
match pos:
case 1: return '1st argument'
case 2: return '2nd argument'
case 3: return '3rd argument'
case _: return f'{pos}th argument'
case 'de':
match pos:
case 1: return 'erstes Argument'
case 2: return 'zweites Argument'
case 3: return 'drittes Argument'
case 4: return 'viertes Argument'
case 5: return 'fünftes Argument'
case 6: return 'sechstes Argument'
case 7: return 'siebtes Argument'
case 8: return 'achtes Argument'
case 9: return 'neuntes Argument'
case _: return f'{pos}. Argument'
case l:
raise ValueError(f'Unexpected language: {l}')
def expectingArgumentOfTy(cn: location.CallableName, ty: str, pos: int | str) -> str:
if isinstance(pos, int):
arg = transArg(pos)
else:
match getLang():
case 'en':
arg = f'argument `{pos}`'
case 'de':
arg = f'Argument `{pos}`'
match cn.kind:
case 'function':
return tr('The call of function `{fun}` expects value of type `{ty}` as {arg}.',
fun=cn.name, ty=ty, arg=arg)
case location.ClassMember('method', cls):
return tr('The call of method `{method}` of class `{cls}` expects value of type `{ty}` as {arg}.',
method=cn.name, cls=cls, ty=ty, arg=arg)
case location.ClassMember('recordConstructor', cls):
return tr('The call of the constructor of record `{cls}` expects value of type `{ty}` as {arg}.',
cls=cls, ty=ty, arg=arg)
raise ValueError(f'Unexpected: {cn}')
def expectingDefaultValueOfTy(cn: location.CallableName, ty: str, paramName: str) -> str:
match cn.kind:
case 'function':
return tr('Default value for parameter `{paramName}` of function `{fun}` must have type `{ty}`.',
paramName=paramName, fun=cn.name, ty=ty)
case location.ClassMember('method', cls):
return tr('Default value for parameter `{paramName}` of method `{method}` in class `{cls}` must have type `{ty}`.',
paramName=paramName, method=cn.name, cls=cls, ty=ty)
case location.ClassMember('recordConstructor', cls):
return tr('Default value for attribute `{paramName}` of record `{cls}` must have type `{ty}`.',
paramName=paramName, cls=cls, ty=ty)
raise ValueError(f'Unexpected: {cn}')
def argCount(n: int) -> str:
match getLang():
case 'en':
if n == 0:
return 'no arguments'
elif n == 1:
return '1 argument'
else:
return f'{n} arguments'
case 'de':
if n == 0:
return 'keine Argumente'
elif n == 1:
return '1 Argument'
else:
return f'{n} Argumente'
def argCountExact(cn: location.CallableName, expected: int) -> str:
match cn.kind:
case 'function':
header = tr('Function `{fun}` takes ', fun=cn.name)
case location.ClassMember('method', cls):
header = tr('Method `{method}` of class `{cls}` takes ',
method=cn.name, cls=cls)
case location.ClassMember('recordConstructor', cls):
header = tr('Constructor of record `{cls}` takes ',
cls=cls)
case _:
raise ValueError(f'Unexpected: {cn}')
return header + argCount(expected) + '.'
def argCountMin(cn: location.CallableName, expected: int) -> str:
match cn.kind:
case 'function':
header = tr('Function `{fun}` takes at least ', fun=cn.name)
case location.ClassMember('method', cls):
header = tr('Method `{method}` of class `{cls}` takes at least ',
method=cn.name, cls=cls)
case location.ClassMember('recordConstructor', cls):
header = tr('Constructor of record `{cls}` takes at least ',
cls=cls)
case _:
raise ValueError(f'Unexpected: {cn}')
return header + argCount(expected) + '.'
def argCountMax(cn: location.CallableName, expected: int) -> str:
match cn.kind:
case 'function':
header = tr('Function `{fun}` takes at most ', fun=cn.name)
case location.ClassMember('method', cls):
header = tr('Method `{method}` of class `{cls}` takes at most ',
method=cn.name, cls=cls)
case location.ClassMember('recordConstructor', cls):
header = tr('Constructor of record `{cls}` takes at most ',
cls=cls)
case _:
raise ValueError(f'Unexpected: {cn}')
return header + argCount(expected) + '.'
def realArgumentTy(ty: str) -> str:
return tr('But the value given has type `{ty}`.', ty=ty)
def realDefaultValueTy(ty: str) -> str:
return tr('But the default value has type `{ty}`.', ty=ty)
def realSetAttrTy(ty: str) -> str:
return tr('Cannot set attribute to value of type `{ty}`.', ty=ty)
def expectingTypeAnnotation(cn: location.CallableName, param: str) -> str:
match cn.kind:
case 'function':
return tr('Parameter `{param}` of function `{fun}` requires a type annotation.',
fun=cn.name, param=param)
case location.ClassMember('method', cls):
return tr('Parameter `{param}` of method `{method}` from class `{cls}` requires a type annotation.',
method=cn.name, cls=cls, param=param)
case location.ClassMember('recordConstructor', cls):
return tr('Parameter `{param}` of constructor of record `{cls}` requires a type annotation.',
cls=cls, param=param)
raise ValueError(f'Unexpected: {cn}')
def noTypeAnnotationForAttribute(attrName: str, recordName: str) -> str:
return tr('Attribute `{name}` of record `{record}` requires a type annotation.',
name=attrName, record=recordName)
def invalidTy(ty: Any) -> str:
return tr('invalid type `{ty}`', ty=ty)
def invalidRestArgTy(ty: Any) -> str:
return tr('invalid type for rest argument: `{ty}`', ty=ty)
def invalidKwArgTy(ty: Any) -> str:
return tr('invalid type for keyword argument: `{ty}`', ty=ty)
def didYouMean(ty: str) -> str:
return tr('Did you mean `{ty}`?', ty=ty)
def recordAttrDeclTy(recordName: str, attrName: str, ty: Any) -> str:
return tr('Attribute `{attrName}` of record `{recordName}` declared with type `{ty}`.',
recordName=recordName, attrName=attrName, ty=ty)
def unknownKeywordArgument(cn: location.CallableName, name: str) -> str:
match cn.kind:
case 'function':
return tr('Function `{fun}` does not accept keyword argument `{name}`.',
fun=cn.name, name=name)
case location.ClassMember('method', cls):
return tr('Method `{method}` from class `{cls}` does not accept keyword argument `{name}`.',
method=cn.name, cls=cls, name=name)
case location.ClassMember('recordConstructor', cls):
return tr('Constructor of record `{cls}` does not accept keyword argument `{name}`.',
cls=cls, name=name)
raise ValueError(f'Unexpected: {cn}')
def checkExpected(expected: str, actual: str) -> str:
return tr('Expected {expected}, but the result is {actual}', expected=expected,actual=actual)
def numTests(n: int) -> str:
match getLang():
case 'en':
if n == 0:
return 'no tests'
elif n == 1:
return '1 test'
else:
return f'{n} tests'
case 'de':
if n == 0:
return 'keine Tests'
elif n == 1:
return '1 Test'
else:
return f'{n} Tests'
def numFailing(n: int) -> str:
match getLang():
case 'en':
if n == 0:
return 'no errors'
elif n == 1:
return '1 error'
else:
return f'{n} errors'
case 'de':
if n == 0:
return 'keine Fehler'
elif n == 1:
return '1 Fehler'
else:
return f'{n} Fehler'