Dash Core  0.12.2.1
P2P Digital Currency
extract_strings_qt.py
Go to the documentation of this file.
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 import operator
9 import os
10 import sys
11 
12 OUT_CPP="qt/dashstrings.cpp"
13 EMPTY=['""']
14 
15 def parse_po(text):
16  """
17  Parse 'po' format produced by xgettext.
18  Return a list of (msgid,msgstr) tuples.
19  """
20  messages = []
21  msgid = []
22  msgstr = []
23  in_msgid = False
24  in_msgstr = False
25 
26  for line in text.split('\n'):
27  line = line.rstrip('\r')
28  if line.startswith('msgid '):
29  if in_msgstr:
30  messages.append((msgid, msgstr))
31  in_msgstr = False
32  # message start
33  in_msgid = True
34 
35  msgid = [line[6:]]
36  elif line.startswith('msgstr '):
37  in_msgid = False
38  in_msgstr = True
39  msgstr = [line[7:]]
40  elif line.startswith('"'):
41  if in_msgid:
42  msgid.append(line)
43  if in_msgstr:
44  msgstr.append(line)
45 
46  if in_msgstr:
47  messages.append((msgid, msgstr))
48 
49  return messages
50 
51 files = sys.argv[1:]
52 
53 # xgettext -n --keyword=_ $FILES
54 XGETTEXT=os.getenv('XGETTEXT', 'xgettext')
55 child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
56 (out, err) = child.communicate()
57 
58 messages = parse_po(out)
59 
60 f = open(OUT_CPP, 'w')
61 f.write("""
62 
63 #include <QtGlobal>
64 
65 // Automatically generated by extract_strings.py
66 #ifdef __GNUC__
67 #define UNUSED __attribute__((unused))
68 #else
69 #define UNUSED
70 #endif
71 """)
72 f.write('static const char UNUSED *dash_strings[] = {\n')
73 messages.sort(key=operator.itemgetter(0))
74 for (msgid, msgstr) in messages:
75  if msgid != EMPTY:
76  f.write('QT_TRANSLATE_NOOP("dash-core", %s),\n' % ('\n'.join(msgid)))
77 f.write('};\n')
78 f.close()