You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extract_strings_qt.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. OUT_CPP="src/qt/bitcoinstrings.cpp"
  11. EMPTY=['""']
  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. 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. msgid = [line[6:]]
  31. elif line.startswith('msgstr '):
  32. in_msgid = False
  33. in_msgstr = True
  34. msgstr = [line[7:]]
  35. elif line.startswith('"'):
  36. if in_msgid:
  37. msgid.append(line)
  38. if in_msgstr:
  39. msgstr.append(line)
  40. if in_msgstr:
  41. messages.append((msgid, msgstr))
  42. return messages
  43. files = glob.glob('src/*.cpp') + glob.glob('src/*.h')
  44. # xgettext -n --keyword=_ $FILES
  45. XGETTEXT=os.getenv('XGETTEXT', 'xgettext')
  46. child = Popen([XGETTEXT,'--output=-','-n','--keyword=_'] + files, stdout=PIPE)
  47. (out, err) = child.communicate()
  48. messages = parse_po(out)
  49. f = open(OUT_CPP, 'w')
  50. f.write("""
  51. #include <QtGlobal>
  52. // Automatically generated by extract_strings.py
  53. #ifdef __GNUC__
  54. #define UNUSED __attribute__((unused))
  55. #else
  56. #define UNUSED
  57. #endif
  58. """)
  59. f.write('static const char UNUSED *bitcoin_strings[] = {\n')
  60. messages.sort(key=operator.itemgetter(0))
  61. for (msgid, msgstr) in messages:
  62. if msgid != EMPTY:
  63. f.write('QT_TRANSLATE_NOOP("bitcoin-core", %s),\n' % ('\n'.join(msgid)))
  64. f.write('};\n')
  65. f.close()