-
Notifications
You must be signed in to change notification settings - Fork 32
/
update-translations.py
executable file
·167 lines (125 loc) · 5.48 KB
/
update-translations.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
#!/usr/bin/python
#****************************************************************************
#**
#** qutIM instant messenger
#**
#** Copyright (c) 2011 Ruslan Nigmatullin <[email protected]>
#**
#*****************************************************************************
#**
#** $QUTIM_BEGIN_LICENSE$
#** This program is free software: you can redistribute it and/or modify
#** it under the terms of the GNU 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 General Public License for more details.
#**
#** You should have received a copy of the GNU General Public License
#** along with this program. If not, see http://www.gnu.org/licenses/.
#** $QUTIM_END_LICENSE$
#**
#****************************************************************************/
import os
import json
import glob
import re
import subprocess
def ensure_command(*commands):
for command in commands:
try:
subprocess.check_call([command, '--help'])
return command
except subprocess.CalledProcessError:
return command
except:
pass
raise RuntimeError('Executables not found: {}'.format(commands))
lupdate = ensure_command('lupdate-qt5', 'lupdate')
lconvert = ensure_command('lconvert-qt5', 'lconvert')
msgmerge = ensure_command('msgmerge')
def is_valid_plugin(path, name):
if not os.path.isdir(path):
return False
return os.path.exists(os.path.join(path, '%s.qbs' % name))
modules = [('core', 'src/bin')]
for plugin_type in os.listdir('src/plugins'):
plugin_type_path = os.path.join('src/plugins', plugin_type)
if not os.path.isdir(plugin_type_path):
continue
for plugin in os.listdir(plugin_type_path):
modules += (os.path.join(plugin_type_path, plugin), plugin)
modules = filter(lambda module: is_valid_plugin(module[0], module[1]), modules)
files_to_remove = set()
def write_string(path, context, strings):
with open(path, "w") as f:
files_to_remove.add(path)
for x in strings:
f.write('Qt::translate("{0}", "{1}");\n'.format(context, x))
strings = set()
for resource in glob.glob("src/share/qutim/webkitstyle/*/Contents/Resources/*.json"):
with open(resource) as f:
try:
for item in json.load(f):
if 'label' in item:
strings.add(item['label'])
except Exception as e:
print '[ERROR] {0}: {1}'.format(resource, e)
write_string('src/plugins/generic/adiumwebview/__custom_json_from_styles.cpp', 'Style', strings)
strings = set()
for root, dirs, files in os.walk('plugins/weather'):
for file in filter(lambda x: x.endswith('.html'), files):
with open(os.path.join(root, file)) as f:
for line in f:
strings.update(map(lambda x: x[:-2], re.findall('(?<=%localized{).*?}%', line)))
write_string('src/plugins/generic/weather/__template_from_weather.cpp', 'Weather', strings)
strings = set()
for root, dirs, files in os.walk('.'):
for file in filter(lambda x: x.endswith('.h'), files):
with open(os.path.join(root, file)) as f:
for line in f:
strings.update(map(lambda x: x.group(1), re.finditer('Q_CLASSINFO.*"Service".*"(.*?)"\\)', line)))
write_string('src/bin/__generated_from_service_names.cpp', 'Service', strings)
authors = set()
tasks = set()
for file in glob.glob('src/bin/contributers/*.json') + glob.glob('src/bin/devels/*.json'):
with open(file) as f:
data = json.load(f)
if 'name' in data:
authors.add(data['name'])
if 'task' in data:
tasks.add(data['task'])
devels_path = '__from_devels_json.cpp'
with open(devels_path, "w") as f:
files_to_remove.add(devels_path)
cpp_strings = []
cpp_strings += map(lambda x: 'Qt::translate("{0}", "{1}");\n'.format('Author', x), authors)
cpp_strings += map(lambda x: 'Qt::translate("{0}", "{1}");\n'.format('Task', x), tasks)
f.writelines(cpp_strings)
modules.append((devels_path, 'devels'))
for module in modules:
strings = set()
for file in glob.glob(os.path.join(module[0], '*.plugin.json')):
with open(file) as f:
data = json.load(f)
for label in ('pluginDescription', 'pluginName'):
if label in data:
strings.add(data[label])
if strings:
write_string(os.path.join(module[0], '__data_from_plugin_json.cpp'), 'Plugin', strings)
module_path = os.path.join('translations/modules', module[1])
if not os.path.exists(module_path):
os.makedirs(module_path)
ts_file = os.path.join(module_path, module[1] + '.ts')
pot_file = os.path.join(module_path, module[1] + '.pot')
subprocess.call([lupdate, '-extensions', 'h,cpp,mm,js,c,ui,qml', '-locations', 'relative', module[0], '-ts', ts_file])
subprocess.call([lconvert, '-i', ts_file, '-o', pot_file])
files_to_remove.add(ts_file)
for po_file in glob.glob(os.path.join(module_path, '*.po')):
language = os.path.basename(po_file)[:-3]
subprocess.call([msgmerge, '--update', '--lang', language, '--backup=off', po_file, pot_file])
for file in files_to_remove:
os.remove(file)