forked from XX-net/XX-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_i18n.py
157 lines (127 loc) · 4.21 KB
/
simple_i18n.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
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
import locale
import os
import sys
import subprocess
class SimpleI18N():
def __init__(self, lang=None):
if lang:
self.lang = lang
else:
self.lang = self.get_os_language()
if not self.lang:
self.lang = "en_US"
def get_os_language(self):
try:
lang_code, code_page = locale.getdefaultlocale()
#('en_GB', 'cp1252'), en_US,
self.lang_code = lang_code
return lang_code
except:
#Mac fail to run this
pass
if sys.platform == "darwin":
try:
oot = os.pipe()
p = subprocess.Popen(["/usr/bin/defaults", 'read', 'NSGlobalDomain', 'AppleLanguages'], stdout=oot[1])
p.communicate()
lang_code = self.get_default_language_code_for_mac(os.read(oot[0], 10000))
self.lang_code = lang_code
return lang_code
except:
pass
lang_code = 'Unknown'
return lang_code
def get_valid_languages(self):
# return ['de_DE', 'en_US', 'es_VE', 'fa_IR', 'ja_JP', 'zh_CN']
return ['en_US', 'fa_IR', 'zh_CN']
def get_default_language_code_for_mac(self, lang_code):
if lang_code.find('zh') != -1:
return 'zh_CN'
elif lang_code.find('en') != -1:
return 'en_US'
elif lang_code.find('fa') != -1:
return 'fa_IR'
else:
return 'Unknown'
def po_loader(self, file):
po_dict = {}
fp = open(file, "r")
while True:
line = fp.readline()
if not line:
break
if len(line) < 2:
continue
if line.startswith("#"):
continue
if line.startswith("msgid "):
key = line[7:-2]
value = ""
while True:
line = fp.readline()
if not line:
break
if line.startswith("\""):
key += line[1:-2]
elif line.startswith("msgstr "):
value += line[8:-2]
break
else:
break
while True:
line = fp.readline()
if not line:
break
if line.startswith("\""):
value += line[1:-2]
else:
break
if key == "":
continue
po_dict[key] = value
return po_dict
def _render(self, po_dict, file):
fp = open(file, "r")
content = fp.read()
out_arr = []
cp = 0
while True:
bp = content.find("{{", cp)
if bp == -1:
break
ep = content.find("}}", bp)
if ep == -1:
print content[bp:]
break
b1p = content.find("_(", bp, ep)
if b1p == -1:
print content[bp:]
continue
b2p = content.find("\"", b1p+2, b1p + 4)
if b2p == -1:
print content[bp:]
continue
e1p = content.find(")", ep - 2, ep)
if e1p == -1:
print content[bp:]
continue
e2p = content.find("\"", e1p - 2, e1p)
if e2p == -1:
print content[bp:]
continue
out_arr.append(content[cp:bp])
key = content[b2p+1:e2p]
if key not in po_dict or po_dict[key] == "":
out_arr.append(key)
else:
out_arr.append(po_dict[key])
cp = ep + 2
out_arr.append(content[cp:])
return "".join(out_arr)
def render(self, lang_path, template_file):
po_file = os.path.join(lang_path, self.lang, "LC_MESSAGES", "messages.po")
if not os.path.isfile(po_file):
return self._render(dict(), template_file)
else:
po_dict = self.po_loader(po_file)
return self._render(po_dict, template_file)