-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathi18n.py
More file actions
55 lines (42 loc) · 1.79 KB
/
i18n.py
File metadata and controls
55 lines (42 loc) · 1.79 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
# coding:utf-8
# i18n module
# Usage:
# 1. run `translation/generate_pot.py`
# 2. edit `translation\locale\zh_CN\LC_MESSAGES\xx.po` in PoEdit or other software (need to apply `.pot` file in PoEdit)
# 3. save `.po` file. the `.mo` file will be generate when GIA start.
import gettext, os, json, locale
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
def load_json(json_name='General.json', default_path='config\\settings') -> dict:
all_path = os.path.join(ROOT_PATH, default_path, json_name)
return json.load(open(all_path, 'r', encoding='utf-8'))
def verify_path(root):
root = os.path.abspath(root)
if not os.path.exists(root):
verify_path(os.path.dirname(root))
os.mkdir(root)
print(f"dir {root} has been created")
jpath = fr"{ROOT_PATH}/config/settings/General.json"
if os.path.exists(jpath):
j = json.load(open(jpath, 'r', encoding='utf-8'))
DEBUG_MODE = j["DEBUG"]
GLOBAL_LANG = j["Lang"]
else:
DEBUG_MODE = False
GLOBAL_LANG = "$locale$"
def get_local_lang():
lang = locale.getdefaultlocale()[0]
print(lang)
if lang in ["zh_CN", "zh_SG", "zh_MO", "zh_HK", "zh_TW"]:
return "zh_CN"
else:
return "en_US"
if GLOBAL_LANG == "$locale$":
GLOBAL_LANG = get_local_lang()
# create .mo files
po_file_path = os.path.join(ROOT_PATH, r'translation/locale', GLOBAL_LANG, 'LC_MESSAGES', f'{GLOBAL_LANG}.po')
mo_file_path = os.path.splitext(po_file_path)[0] + '.mo'
print(fr'python "{ROOT_PATH}/translation/msgfmt.py" -o "{mo_file_path}" "{po_file_path}"')
os.system(fr'python "{ROOT_PATH}/translation/msgfmt.py" -o "{mo_file_path}" "{po_file_path}"')
l10n = gettext.translation(GLOBAL_LANG, localedir=os.path.join(ROOT_PATH, r"translation/locale"), languages=[GLOBAL_LANG])
l10n.install()
t2t = l10n.gettext