-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs2pot.py
executable file
·193 lines (150 loc) · 6.09 KB
/
js2pot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2016 - Walter Bender <[email protected]>
# Create or update pot file based on js files.
# How to use: script.py js-path pot-file
# Outputs to pot-file_
import os
import re
import sys
import glob
import codecs
js_list = []
js_trans_dict = {} # //.TRANS:
js_line_numbers = []
js_line_numbers_dict = {}
def mine_path(js_files, root_path):
trans_note = []
for path in js_files:
basename = os.path.basename(path)
js_fd = codecs.open(path, "r", "UTF-8")
count = 1
for line in js_fd:
tmp = line.split('//.TRANS:')
if len(tmp) > 1:
if not tmp[1] in trans_note:
trans_note.append(tmp[1])
tmp = line.split('_("')
if len(tmp) > 1:
for i in range(len(tmp)):
if i == 0:
continue
phrase = tmp[i].split('")')[0];
line_number = '%s/%s:%d' % (root_path, basename, count)
if phrase in js_list:
if not line_number in js_line_numbers_dict[phrase]:
js_line_numbers_dict[phrase].append(line_number)
else:
js_list.append(phrase)
js_line_numbers_dict[phrase] = [line_number]
if len(trans_note) > 0:
if phrase in js_trans_dict:
for note in trans_note:
js_trans_dict[phrase].append(note)
else:
js_trans_dict[phrase] = trans_note
trans_note = []
tmp = line.split("_('")
if len(tmp) > 1:
for i in range(len(tmp)):
if i == 0:
continue
phrase = tmp[i].split("')")[0];
line_number = '%s/%s:%d' % (root_path, basename, count)
if phrase in js_list:
if not line_number in js_line_numbers_dict[phrase]:
js_line_numbers_dict[phrase].append(line_number)
else:
js_list.append(phrase)
js_line_numbers_dict[phrase] = [line_number]
if len(trans_note) > 0:
if phrase in js_trans_dict:
for note in trans_note:
js_trans_dict[phrase].append(note)
else:
js_trans_dict[phrase] = trans_note
trans_note = []
count += 1
def mine_js_files(js_pathname, pot_filename):
# Read data from the existing POT file
pot_fd = codecs.open(pot_filename, "r", "UTF-8")
pot_list = []
trans_note = []
pot_trans_dict = {} # //.TRANS:
pot_line_numbers = []
pot_line_numbers_dict = {}
pot_header = ''
end_of_header = False;
for line in pot_fd:
if line[0:2] == '#:':
pot_line_numbers.append(line)
end_of_header = True
elif line[0:2] == '#.':
trans_note.append(line)
end_of_header = True
elif line[0:5] == 'msgid':
key = line[7:-2]
pot_list.append(key)
if not key == '':
end_of_header = True
if len(trans_note) > 0:
pot_trans_dict[key] = trans_note
trans_note = []
if len(pot_line_numbers) > 0:
pot_line_numbers_dict[key] = pot_line_numbers
pot_line_numbers = []
if not end_of_header:
pot_header += line
js_files = glob.glob(os.path.join(js_pathname, 'js', '*js'))
print(js_files)
mine_path(js_files, "js")
js_files = glob.glob(os.path.join(js_pathname, 'js/utils', '*js'))
print(js_files)
mine_path(js_files, "js/utils")
js_files = glob.glob(os.path.join(js_pathname, 'js/blocks', '*js'))
print(js_files)
mine_path(js_files, "js/blocks")
js_files = glob.glob(os.path.join(js_pathname, 'js/turtleactions', '*js'))
print(js_files)
mine_path(js_files, "js/turtleactions")
js_files = glob.glob(os.path.join(js_pathname, 'js/widgets', '*js'))
print(js_files)
mine_path(js_files, "js/widgets")
js_files = glob.glob(os.path.join(js_pathname, 'js/js-export', '*js'))
print(js_files)
mine_path(js_files, "js/js-export")
js_files = glob.glob(os.path.join(js_pathname, 'planet/js', '*js'))
print(js_files)
mine_path(js_files, "planet/js")
rtp_files = glob.glob(os.path.join(js_pathname, 'plugins', '*rtp'))
print(rtp_files)
mine_path(rtp_files, "plugins")
output = codecs.open(pot_filename + '_', 'w', "UTF-8")
output.write(pot_header)
for i in range(len(js_list)):
for j in range(len(js_line_numbers_dict[js_list[i]])):
output.write('#: %s\n' % (js_line_numbers_dict[js_list[i]][j]))
if js_list[i] in js_trans_dict:
for j in range(len(js_trans_dict[js_list[i]])):
output.write('#.TRANS:%s' % (js_trans_dict[js_list[i]][j]))
new_phrase = js_list[i].replace('"', '\\"')
if len(new_phrase) > 0:
output.write('msgid "%s"\nmsgstr ""\n\n' % (new_phrase))
else:
output.write('\n')
for i in range(len(pot_list)):
if pot_list[i] in js_list:
continue;
if pot_list[i] in pot_line_numbers_dict:
for j in range(len(pot_line_numbers_dict[pot_list[i]])):
output.write('%s' % (pot_line_numbers_dict[pot_list[i]][j]))
if pot_list[i] in pot_trans_dict:
for j in range(len(pot_trans_dict[pot_list[i]])):
output.write('%s' % (pot_trans_dict[pot_list[i]][j]))
output.write('#~msgid "%s"\n#~msgstr ""\n\n' % (pot_list[i]))
output.close()
if __name__ == '__main__':
if len(sys.argv) != 3:
print('usage is: python js2pot project_path pot_path')
else:
ini = mine_js_files(sys.argv[1], sys.argv[2])