6627de4abf48b945658fec4c5c230c6014eb10b1
[novacoin.git] / scripts / 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
8 OUT_CPP="src/qt/bitcoinstrings.cpp"
9 EMPTY=['""']
10
11 def parse_po(text):
12     """
13     Parse 'po' format produced by xgettext.
14     Return a list of (msgid,msgstr) tuples.
15     """
16     messages = []
17     msgid = []
18     msgstr = []
19     in_msgid = False
20     in_msgstr = False
21
22     for line in text.split('\n'):
23         line = line.rstrip('\r')
24         if line.startswith('msgid '):
25             if in_msgstr:
26                 messages.append((msgid, msgstr))
27                 in_msgstr = False
28             # message start
29             in_msgid = True
30             
31             msgid = [line[6:]]
32         elif line.startswith('msgstr '):
33             in_msgid = False
34             in_msgstr = True
35             msgstr = [line[7:]]
36         elif line.startswith('"'):
37             if in_msgid:
38                 msgid.append(line)
39             if in_msgstr:
40                 msgstr.append(line)
41
42     if in_msgstr:
43         messages.append((msgid, msgstr))
44
45     return messages
46
47 files = ['src/base58.h', 'src/bignum.h', 'src/db.cpp', 'src/db.h', 'src/headers.h', 'src/init.cpp', 'src/init.h', 'src/irc.cpp', 'src/irc.h', 'src/key.h', 'src/main.cpp', 'src/main.h', 'src/net.cpp', 'src/net.h', 'src/noui.h', 'src/script.cpp', 'src/script.h', 'src/serialize.h', 'src/strlcpy.h', 'src/uint256.h', 'src/util.cpp', 'src/util.h']
48
49 # xgettext -n --keyword=_ $FILES
50 child = Popen(['xgettext','--output=-','-n','--keyword=_'] + files, stdout=PIPE)
51 (out, err) = child.communicate()
52
53 messages = parse_po(out) 
54
55 f = open(OUT_CPP, 'w')
56 f.write('#include <QtGlobal>\n')
57 f.write('// Automatically generated by extract_strings.py\n')
58 f.write('static const char *bitcoin_strings[] = {')
59 for (msgid, msgstr) in messages:
60     if msgid != EMPTY:
61         f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
62 f.write('};')
63 f.close()