forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tool.py
316 lines (228 loc) · 7.45 KB
/
tool.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
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
# -*- coding: utf-8 -*-
import polib
import re
import os
import time
from path import Path
from shutil import move
from tempfile import mkstemp
from os import remove, walk
BASE_DIR = Path('.').abspath()
HEADER = """
# edX translation file
# Copyright (C) 2018 edX
# This file is distributed under the GNU AFFERO GENERAL PUBLIC LICENSE.
# EdX Team <[email protected]>, 2018.
#
"""
CONFIG = {
'edx': 'conf/locale/en/LC_MESSAGES',
'xadmin': 'eliteu_admin/lib/xadmin/locale/zh_CN/LC_MESSAGES',
'membership': '/Users/yingli/Documents/Github/docker-edx/edx-membership/conf/locale/zh_CN/LC_MESSAGES'
}
def valid(text, pattern):
"""
Use re to match text to decide whether text is valid
:param text: str
:param pattern: re compiled rule
:return: True if match and False if mismatch
"""
match = pattern.search(text)
return match
def check(path):
"""
Use for extract function.
If file doesn't exists, create.
If file exists, truncate.
:param path: path of file
:return: Pofile of path
"""
f = open(path, 'w')
# f.write(HEADER)
f.close()
pomsgs = polib.pofile(path)
return pomsgs
def trans_unicode(source_file_path):
"""
Cause some translate string would be Escaped to unicode,
use the function to escaped unicode to cn.
:param source_file_path: path of file
:return:
"""
fh, target_file_path = mkstemp()
source = polib.pofile(source_file_path)
target = open(target_file_path, 'w')
target.close()
target_po = polib.pofile(target_file_path)
for s in source:
if "\\u" in s.msgid:
s.msgid = s.msgid.encode('utf-8').decode('unicode_escape')
target_po.append(s)
target_po.save()
remove(source_file_path)
move(target_file_path, source_file_path)
def update(source_file, target_file):
"""
Update trans of to target_file from source_file
:param source_file: File contains msgstr u need
:param target_file: File which need to be updated msgstr
:return:
"""
import polib
source = polib.pofile(source_file)
target = polib.pofile(target_file)
target_ids = [(t.msgid, t.msgid_plural, t.msgctxt) for t in target]
target_msgctxt = [(t.msgid, t.msgctxt) for t in target]
length = len(source)
for i in range(0, length):
s = source[i]
msgid = s.msgid
msgstr = s.msgstr
msgctxt = s.msgctxt
msgid_plural = s.msgid_plural
msgstr_plural = s.msgstr_plural
try:
# pgettext
if msgctxt is not None:
index = target_msgctxt.index((msgid, msgctxt))
t = target[index]
t.msgstr = msgstr
# plural
elif msgid_plural != "":
index = target_ids.index((msgid, msgid_plural, msgctxt))
t = target[index]
for k in t.msgstr_plural.keys():
t.msgstr_plural[k] = msgstr_plural[k]
# ordinary
elif msgstr != "":
index = target_ids.index((msgid, '', None))
t = target[index]
t.msgstr = msgstr
except:
continue
target.save()
def exchange(source_file_path):
"""
Exchange msgid and msgstr position of zh.po
:param source_file_path: File which need to be exchange
:return:
"""
source = polib.pofile(source_file_path)
fh, target_file_path = mkstemp()
target = open(target_file_path, 'w')
target.close()
target = polib.pofile(target_file_path)
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
for s in source:
if valid(s.msgid.strip(), zhPattern):
s.msgid, s.msgstr = s.msgstr, s.msgid
target.append(s)
target.save()
remove(source_file_path)
move(target_file_path, source_file_path)
def extract(project):
"""
Extract chinese msgid from po files
:param project:
:return:
"""
names = ['django.po', 'djangojs.po']
path = CONFIG.get(project)
for name in names:
spath = os.path.join(path, name)
tpath = os.path.join(path, 'zh-' + name)
source = polib.pofile(spath)
target = check(tpath)
for msg in source:
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
text = msg.msgid.strip()
if valid(text, zhPattern):
target.append(msg)
target.sort(key=lambda x: len(x.msgid), reverse=True)
target.save()
def replace(source_file_path, pattern, substring):
"""
Update msgid with specified pattern and replace to substring
:param source_file_path:
:param pattern:
:param substring:
:return:
"""
fh, target_file_path = mkstemp()
target_file = open(target_file_path, 'w')
source_file = open(source_file_path, 'r')
# print(source_file_path, pattern, substring)
for line in source_file:
# TODO: 待处理
if pattern in line:
if line.lstrip().startswith('#'):
target_file.write(line)
elif line.lstrip().startswith("//"):
target_file.write(line)
elif line.lstrip().startswith('<! --'):
target_file.write(line)
else:
target_file.write(line.replace(pattern, substring))
else:
target_file.write(line)
target_file.close()
source_file.close()
remove(source_file_path)
move(target_file_path, source_file_path)
def replace_code(project):
path = CONFIG.get(project)
names = ['zh-django.po', 'zh-djangojs.po']
source = [os.path.join(path, name) for name in names]
for s in source:
pomsgs = polib.pofile(s)
for msg in pomsgs:
msgid = msg.msgid.encode('utf-8')
msgstr = msg.msgstr.encode('utf-8')
# A list of file path
occurrences = msg.occurrences
for (path, line) in occurrences:
fpath = os.path.join(BASE_DIR, path)
if os.path.exists(fpath) and msgstr != '':
replace(fpath, msgid, msgstr)
def find(project):
names = ['django.po']
path = CONFIG.get(project)
for name in names:
spath = os.path.join(path, name)
tpath = os.path.join(path, 'zh-' + name)
source = polib.pofile(spath)
target = check(tpath)
for msg in source:
if msg.msgstr == "":
target.append(msg)
target.save()
def i18n_dirty_check(path):
flag = False
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
source = polib.pofile(path)
for msg in source:
text = msg.msgid.strip()
match = zhPattern.search(text)
if match:
flag = True
break
return flag
def main():
path = 'conf/locale/zh_CN/LC_MESSAGES'
files= os.listdir(path)
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
po = []
for f in files:
if os.path.splitext(f)[1] == '.po':
po.append(f)
popath = [os.path.join(path, p) for p in po]
for path in popath:
exchange(path)
if i18n_dirty_check(path) is True:
print("%s is dirty.") % path
# main()
# exchange('conf/locale/zh_CN/LC_MESSAGES/djangojs1.po')
# exchange('conf/locale/zh_CN/LC_MESSAGES/django.po')
# update('conf/locale/zh_CN/LC_MESSAGES/djangojs1.po', 'conf/locale/zh_CN/LC_MESSAGES/djangojs.po')
# update('conf/locale/zh_CN/LC_MESSAGES/django1.po', 'conf/locale/zh_CN/LC_MESSAGES/django.po')
extract('edx')