aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/import_po.py
blob: 7e99064be9b1597d8841de13b131948cebbff878 (plain) (blame)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Download translations from transifex and regenerate files."""

from __future__ import unicode_literals, print_function
import codecs
from glob import glob
import os
import sys
import polib

if 'nopull' not in sys.argv:
    os.system("tx pull -a")

trans_files = glob(os.path.join('translations', 'nikola.messages', '*.po'))
for fname in trans_files:
    lang = os.path.splitext(os.path.basename(fname))[0].lower()
    outf = os.path.join('nikola', 'data', 'themes', 'base',
                        'messages', 'messages_{0}.py'.format(lang))
    po = polib.pofile(fname)
    lines = """# -*- encoding:utf-8 -*-
from __future__ import unicode_literals

MESSAGES = {""".splitlines()
    lines2 = []
    for entry in po:
        lines2.append('    "{0}": "{1}",'. format(entry.msgid, entry.msgstr))
        ### BACKWARDS COMPATIBILITY PATCH START
        ### TODO: remove in v7
        if entry.msgid in ["Posted:", "Also available in:"]:
            fid = entry.msgid
            fid = fid.replace(':', '')
            fstr = entry.msgstr
            fstr = fstr.replace(':', '').replace(':', '')
            lines2.append('    "{0}": "{1}",'. format(fid, fstr))
        elif entry.msgid == 'More posts about %s':
            fid = entry.msgid
            fid = fid.replace(' %s', '')
            fstr = entry.msgstr
            fstr = fstr.replace(' %s', '').replace('%s', '')
            lines2.append('    "{0}": "{1}",'. format(fid, fstr))
        ### BACKWARDS COMPATIBILITY PATCH END
        ### TODO: remove in v7
    lines.extend(sorted(lines2))
    lines.append("}\n")
    print("Generating:", outf)
    with codecs.open(outf, "wb+", "utf8") as outfile:
        outfile.write('\n'.join(lines))