-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation_pot.py
executable file
·86 lines (80 loc) · 3.09 KB
/
translation_pot.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
#!/usr/bin/env python3
#
# Copyright (C) 2004-2016 ZNC, see the NOTICE file for details.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import glob
import os
import re
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument('--include_dir', action='store')
parser.add_argument('--explicit_sources', action='append')
parser.add_argument('--tmpl_dirs', action='append')
parser.add_argument('--strip_prefix', action='store')
parser.add_argument('--tmp_prefix', action='store')
parser.add_argument('--output', action='store')
args = parser.parse_args()
pot_list = []
# .tmpl
tmpl_pot = args.tmp_prefix + '_tmpl.pot'
tmpl_uniq_pot = args.tmp_prefix + '_tmpl_uniq.pot'
tmpl = []
pattern = re.compile(r'<\?\s*(?:FORMAT|(PLURAL))\s+(?:CTX="([^"]+?)"\s+)?"([^"]+?)"(?(1)\s+"([^"]+?)"|).*?\?>')
for tmpl_dir in args.tmpl_dirs:
for fname in glob.iglob(tmpl_dir + '/*.tmpl'):
fbase = fname[len(args.strip_prefix):]
with open(fname) as f:
for linenum, line in enumerate(f):
for x in pattern.finditer(line):
text, plural, context = x.group(3), x.group(4), x.group(2)
tmpl.append('#: {}:{}'.format(fbase, linenum + 1))
if context:
tmpl.append('msgctxt "{}"'.format(context))
tmpl.append('msgid "{}"'.format(text))
if plural:
tmpl.append('msgid_plural "{}"'.format(plural))
tmpl.append('msgstr[0] ""')
tmpl.append('msgstr[1] ""')
else:
tmpl.append('msgstr ""')
tmpl.append('')
if tmpl:
with open(tmpl_pot, 'w') as f:
print('msgid ""', file=f)
print('msgstr ""', file=f)
print(r'"Content-Type: text/plain; charset=UTF-8\n"', file=f)
print(r'"Content-Transfer-Encoding: 8bit\n"', file=f)
print(file=f)
for line in tmpl:
print(line, file=f)
subprocess.check_call(['msguniq', '-o', tmpl_uniq_pot, tmpl_pot])
pot_list.append(tmpl_uniq_pot)
# .cpp
main_pot = args.tmp_prefix + '_main.pot'
subprocess.check_call(['xgettext',
'--omit-header',
'-D', args.include_dir,
'-o', main_pot,
'--keyword=t:1,1t', '--keyword=t:1,2c,2t',
'--keyword=f:1,1t', '--keyword=f:1,2c,2t',
'--keyword=p:1,2,3t', '--keyword=p:1,2,4c,4t',
'--keyword=d:1,1t', '--keyword=d:1,2c,2t',
] + args.explicit_sources)
if os.path.isfile(main_pot):
pot_list.append(main_pot)
# combine
if pot_list:
subprocess.check_call(['msgcat', '-o', args.output] + pot_list)