]>
jfr.im git - irc/znc/znc.git/blob - translation_pot.py
3 # Copyright (C) 2004-2024 ZNC, see the NOTICE file for details.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
24 parser
= argparse
.ArgumentParser()
25 parser
.add_argument('--include_dir', action
='store')
26 parser
.add_argument('--explicit_sources', action
='append')
27 parser
.add_argument('--tmpl_dirs', action
='append')
28 parser
.add_argument('--strip_prefix', action
='store')
29 parser
.add_argument('--tmp_prefix', action
='store')
30 parser
.add_argument('--output', action
='store')
31 args
= parser
.parse_args()
36 tmpl_pot
= args
.tmp_prefix
+ '_tmpl.pot'
37 tmpl_uniq_pot
= args
.tmp_prefix
+ '_tmpl_uniq.pot'
39 pattern
= re
.compile(r
'<\?\s*(?:FORMAT|(PLURAL))\s+(?:CTX="([^"]+?)"\s+)?"([^"]+?)"(?(1)\s+"([^"]+?)"|).*?(?:"TRANSLATORS:\s*([^"]+?)")?\s*\?>')
40 for tmpl_dir
in args
.tmpl_dirs
:
41 for fname
in glob
.iglob(tmpl_dir
+ '/*.tmpl'):
42 fbase
= fname
[len(args
.strip_prefix
):]
43 with open(fname
, 'rt', encoding
='utf8') as f
:
44 for linenum
, line
in enumerate(f
):
45 for x
in pattern
.finditer(line
):
46 text
, plural
, context
, comment
= x
.group(3), x
.group(4), x
.group(2), x
.group(5)
48 tmpl
.append('# {}'.format(comment
))
49 tmpl
.append('#: {}:{}'.format(fbase
, linenum
+ 1))
51 tmpl
.append('msgctxt "{}"'.format(context
))
52 tmpl
.append('msgid "{}"'.format(text
))
54 tmpl
.append('msgid_plural "{}"'.format(plural
))
55 tmpl
.append('msgstr[0] ""')
56 tmpl
.append('msgstr[1] ""')
58 tmpl
.append('msgstr ""')
61 # Bundle header to .tmpl, even if there were no .tmpl files.
62 # Some .tmpl files contain non-ASCII characters, and the header is needed
63 # anyway, because it's omitted from xgettext call below.
64 with open(tmpl_pot
, 'wt', encoding
='utf8') as f
:
65 print('msgid ""', file=f
)
66 print('msgstr ""', file=f
)
67 print(r
'"Content-Type: text/plain; charset=UTF-8\n"', file=f
)
68 print(r
'"Content-Transfer-Encoding: 8bit\n"', file=f
)
72 subprocess
.check_call(['msguniq', '--force-po', '-o', tmpl_uniq_pot
, tmpl_pot
])
73 pot_list
.append(tmpl_uniq_pot
)
76 main_pot
= args
.tmp_prefix
+ '_main.pot'
77 subprocess
.check_call(['xgettext',
79 '-D', args
.include_dir
,
81 '--keyword=t_s:1,1t', '--keyword=t_s:1,2c,2t',
82 '--keyword=t_f:1,1t', '--keyword=t_f:1,2c,2t',
83 '--keyword=t_p:1,2,3t', '--keyword=t_p:1,2,4c,4t',
84 '--keyword=t_d:1,1t', '--keyword=t_d:1,2c,2t',
85 ] + args
.explicit_sources
)
86 if os
.path
.isfile(main_pot
):
87 pot_list
.append(main_pot
)
91 subprocess
.check_call(['msgcat', '-o', args
.output
] + pot_list
)