[tests] Fix incorrect memory_cleanse(…) call in crypto_tests.cpp
[bitcoinplatinum.git] / share / qt / extract_strings_qt.py
blob5492fdb8c552418040aed93f2a763e4ec0f8e7a2
1 #!/usr/bin/env python
2 # Copyright (c) 2012-2016 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 '''
6 Extract _("...") strings for translation and convert to Qt stringdefs so that
7 they can be picked up by Qt linguist.
8 '''
9 from __future__ import division,print_function,unicode_literals
10 from subprocess import Popen, PIPE
11 import operator
12 import os
13 import sys
15 OUT_CPP="qt/bitcoinstrings.cpp"
16 EMPTY=['""']
18 def parse_po(text):
19 """
20 Parse 'po' format produced by xgettext.
21 Return a list of (msgid,msgstr) tuples.
22 """
23 messages = []
24 msgid = []
25 msgstr = []
26 in_msgid = False
27 in_msgstr = False
29 for line in text.split('\n'):
30 line = line.rstrip('\r')
31 if line.startswith('msgid '):
32 if in_msgstr:
33 messages.append((msgid, msgstr))
34 in_msgstr = False
35 # message start
36 in_msgid = True
38 msgid = [line[6:]]
39 elif line.startswith('msgstr '):
40 in_msgid = False
41 in_msgstr = True
42 msgstr = [line[7:]]
43 elif line.startswith('"'):
44 if in_msgid:
45 msgid.append(line)
46 if in_msgstr:
47 msgstr.append(line)
49 if in_msgstr:
50 messages.append((msgid, msgstr))
52 return messages
54 files = sys.argv[1:]
56 # xgettext -n --keyword=_ $FILES
57 XGETTEXT=os.getenv('XGETTEXT', 'xgettext')
58 if not XGETTEXT:
59 print('Cannot extract strings: xgettext utility is not installed or not configured.',file=sys.stderr)
60 print('Please install package "gettext" and re-run \'./configure\'.',file=sys.stderr)
61 exit(1)
62 child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
63 (out, err) = child.communicate()
65 messages = parse_po(out.decode('utf-8'))
67 f = open(OUT_CPP, 'w')
68 f.write("""
70 #include <QtGlobal>
72 // Automatically generated by extract_strings_qt.py
73 #ifdef __GNUC__
74 #define UNUSED __attribute__((unused))
75 #else
76 #define UNUSED
77 #endif
78 """)
79 f.write('static const char UNUSED *bitcoin_strings[] = {\n')
80 f.write('QT_TRANSLATE_NOOP("bitcoin-core", "%s"),\n' % (os.getenv('PACKAGE_NAME'),))
81 f.write('QT_TRANSLATE_NOOP("bitcoin-core", "%s"),\n' % (os.getenv('COPYRIGHT_HOLDERS'),))
82 if os.getenv('COPYRIGHT_HOLDERS_SUBSTITUTION') != os.getenv('PACKAGE_NAME'):
83 f.write('QT_TRANSLATE_NOOP("bitcoin-core", "%s"),\n' % (os.getenv('COPYRIGHT_HOLDERS_SUBSTITUTION'),))
84 messages.sort(key=operator.itemgetter(0))
85 for (msgid, msgstr) in messages:
86 if msgid != EMPTY:
87 f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
88 f.write('};\n')
89 f.close()