1#!/usr/bin/env python3
2# Copyright (c) 2012-2018 The Bitcoin Core developers
3# Distributed under the MIT software license, see the accompanying
4# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5'''
6Extract _("...") strings for translation and convert to Qt stringdefs so that
7they can be picked up by Qt linguist.
8'''
9from subprocess import Popen, PIPE
10import operator
11import os
12import sys
13
14OUT_CPP="qt/bitcoinstrings.cpp"
15EMPTY=['""']
16
17def parse_po(text):
18    """
19    Parse 'po' format produced by xgettext.
20    Return a list of (msgid,msgstr) tuples.
21    """
22    messages = []
23    msgid = []
24    msgstr = []
25    in_msgid = False
26    in_msgstr = False
27
28    for line in text.split('\n'):
29        line = line.rstrip('\r')
30        if line.startswith('msgid '):
31            if in_msgstr:
32                messages.append((msgid, msgstr))
33                in_msgstr = False
34            # message start
35            in_msgid = True
36
37            msgid = [line[6:]]
38        elif line.startswith('msgstr '):
39            in_msgid = False
40            in_msgstr = True
41            msgstr = [line[7:]]
42        elif line.startswith('"'):
43            if in_msgid:
44                msgid.append(line)
45            if in_msgstr:
46                msgstr.append(line)
47
48    if in_msgstr:
49        messages.append((msgid, msgstr))
50
51    return messages
52
53files = sys.argv[1:]
54
55# xgettext -n --keyword=_ $FILES
56XGETTEXT=os.getenv('XGETTEXT', 'xgettext')
57if not XGETTEXT:
58    print('Cannot extract strings: xgettext utility is not installed or not configured.',file=sys.stderr)
59    print('Please install package "gettext" and re-run \'./configure\'.',file=sys.stderr)
60    sys.exit(1)
61child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
62(out, err) = child.communicate()
63
64messages = parse_po(out.decode('utf-8'))
65
66f = open(OUT_CPP, 'w', encoding="utf8")
67f.write("""
68
69#include <QtGlobal>
70
71// Automatically generated by extract_strings_qt.py
72#ifdef __GNUC__
73#define UNUSED __attribute__((unused))
74#else
75#define UNUSED
76#endif
77""")
78f.write('static const char UNUSED *bitcoin_strings[] = {\n')
79f.write('QT_TRANSLATE_NOOP("bitcoin-core", "%s"),\n' % (os.getenv('COPYRIGHT_HOLDERS'),))
80messages.sort(key=operator.itemgetter(0))
81for (msgid, msgstr) in messages:
82    if msgid != EMPTY:
83        f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
84f.write('};\n')
85f.close()
86