Update to 0.3.0 (New upstream + new RPC calls)
[novacoin.git] / share / qt / extract_strings_qt.py
1 #!/usr/bin/python
2 '''
3 Extract _("...") strings for translation and convert to Qt4 stringdefs so that
4 they can be picked up by Qt linguist.
5 '''
6 from subprocess import Popen, PIPE
7 import glob
8
9 OUT_CPP="src/qt/bitcoinstrings.cpp"
10 EMPTY=['""']
11
12 def parse_po(text):
13     """
14     Parse 'po' format produced by xgettext.
15     Return a list of (msgid,msgstr) tuples.
16     """
17     messages = []
18     msgid = []
19     msgstr = []
20     in_msgid = False
21     in_msgstr = False
22
23     for line in text.split('\n'):
24         line = line.rstrip('\r')
25         if line.startswith('msgid '):
26             if in_msgstr:
27                 messages.append((msgid, msgstr))
28                 in_msgstr = False
29             # message start
30             in_msgid = True
31             
32             msgid = [line[6:]]
33         elif line.startswith('msgstr '):
34             in_msgid = False
35             in_msgstr = True
36             msgstr = [line[7:]]
37         elif line.startswith('"'):
38             if in_msgid:
39                 msgid.append(line)
40             if in_msgstr:
41                 msgstr.append(line)
42
43     if in_msgstr:
44         messages.append((msgid, msgstr))
45
46     return messages
47
48 files = glob.glob('src/*.cpp') + glob.glob('src/*.h') 
49
50 # xgettext -n --keyword=_ $FILES
51 child = Popen(['xgettext','--output=-','-n','--keyword=_'] + files, stdout=PIPE)
52 (out, err) = child.communicate()
53
54 messages = parse_po(out) 
55
56 f = open(OUT_CPP, 'w')
57 f.write("""#include <QtGlobal>
58 // Automatically generated by extract_strings.py
59 #ifdef __GNUC__
60 #define UNUSED __attribute__((unused))
61 #else
62 #define UNUSED
63 #endif
64 """)
65 f.write('static const char UNUSED *bitcoin_strings[] = {')
66 for (msgid, msgstr) in messages:
67     if msgid != EMPTY:
68         f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
69 f.write('};')
70 f.close()