-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.py
63 lines (50 loc) · 1.7 KB
/
language.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
"""
Handle the multi-language system
"""
import json
import os
from collections.abc import Iterable
class Language(dict):
"""
Represent the strings in the proper language
"""
def __init__(self, file: str):
"""
Initialize the language object
:param file: the json file containing translations
"""
self._file = file
with open(file, 'r', encoding="utf-8") as f:
datas = json.loads(f.read())
if os.name != 'posix':
import ctypes
import locale
windll = ctypes.windll.kernel32
language = locale.windows_locale[windll.GetUserDefaultUILanguage()]
else:
language = os.environ['LANG'].split('.')[0]
for key, value in datas.items():
if language in value.keys():
self[key] = value[language]
elif 'en_GB' in value.keys():
self[key] = value['en_GB']
else:
self[key] = f'missing translation for {repr(key)}'
super().__init__()
def __repr__(self):
return f"<Language object on file {repr(self._file)} : " + \
", ".join(f"{repr(var)}={repr(value)}" for var, value in self.items()) + '>'
def __str__(self):
return super().__repr__()
def __getattr__(self, item):
if item in self:
return self[item]
raise AttributeError(str(item))
def __setattr__(self, key, value):
self[key] = value
def __delattr__(self, item):
if item in self:
del self[item]
raise AttributeError(str(item))
def __dir__(self) -> Iterable[str]:
return list(self.keys()) + list(super().__dir__())