forked from lifegpc/bili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang.py
97 lines (87 loc) · 2.9 KB
/
lang.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
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import polib
from os.path import exists
import platform
import ctypes
from typing import Dict
LanDict = Dict[str, str]
dll = None
lan = {'en': 'English', 'ja': '日本語', 'zh_CN': '中文(中国)', 'zh_TW': '中文(臺灣)'}
syslan = None
def getdict(sn: str, lan: str, sn2: str = "bili") -> LanDict:
"""获取翻译词典
sn 资源名称
lan 语言代码"""
if lan == "en":
fn = f"Language/{sn2}.{sn}.pot"
else:
fn = f"Language/{sn2}.{sn}.{lan}.po"
if not exists(fn):
print(f'Can not find the language resource file:"{fn}"')
fn = f'Language/{sn2}.{sn}.pot'
if not exists(fn):
print(f'Can not find the language resource file:"{fn}"')
return -1
po = polib.pofile(fn, encoding='utf8')
r = {}
for i in po.translated_entries():
r[i.msgctxt] = i.msgstr
for i in po.untranslated_entries():
r[i.msgctxt] = i.msgid
return r
def getsyslan(d: bool = False):
"""获取系统语言信息
语言代码:https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/a29e5c28-9fb9-4c49-8e43-4b9b8e733a05
d 是否为调试模式"""
s = platform.system()
if s == "Windows":
global dll
if dll is None:
dll = ctypes.windll.kernel32
la = dll.GetSystemDefaultUILanguage()
if d:
print(f"SystemDefaultUILanguage:{hex(la)}")
if la == 0x804 or la == 0x4 or la == 0x1004 or la == 0x1404:
r = "zh_CN"
elif la == 0x404 or la == 0xc04 or la == 0x7c04:
r = "zh_TW"
elif la == 0x411:
r = "ja"
else:
r = "en"
if d:
global lan
print(f'Choose {r} : {lan[r]}')
return r
else:
return "en" # 非Windows系统默认英文
def getlan(se: dict, ip: dict) -> str:
global syslan
if syslan is None:
try:
syslan = getsyslan()
except:
syslan = "en"
la = syslan
if 'lan' in se:
la = se['lan']
if 'lan' in ip:
la = ip['lan']
return la
if __name__ == "__main__":
print(getdict('start', 'en'))
print(getdict('start', getsyslan(True))) # 测试是否工作