extract_strings_qt.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python
  2. # ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  3. from subprocess import Popen, PIPE
  4. import glob
  5. OUT_CPP="src/qt/ecoinstrings.cpp"
  6. EMPTY=['""']
  7. def parse_po(text):
  8. messages = []
  9. msgid = []
  10. msgstr = []
  11. in_msgid = False
  12. in_msgstr = False
  13. for line in text.split('\n'):
  14. line = line.rstrip('\r')
  15. if line.startswith('msgid '):
  16. if in_msgstr:
  17. messages.append((msgid, msgstr))
  18. in_msgstr = False
  19. in_msgid = True
  20. msgid = [line[6:]]
  21. elif line.startswith('msgstr '):
  22. in_msgid = False
  23. in_msgstr = True
  24. msgstr = [line[7:]]
  25. elif line.startswith('"'):
  26. if in_msgid:
  27. msgid.append(line)
  28. if in_msgstr:
  29. msgstr.append(line)
  30. if in_msgstr:
  31. messages.append((msgid, msgstr))
  32. return messages
  33. files = glob.glob('src/*.cpp') + glob.glob('src/*.h')
  34. child = Popen(['xgettext','--output=-','-n','--keyword=_'] + files, stdout=PIPE)
  35. (out, err) = child.communicate()
  36. messages = parse_po(out)
  37. f = open(OUT_CPP, 'w')
  38. f.write("""#include <QtGlobal>
  39. // Automatically generated by extract_strings.py
  40. #ifdef __GNUC__
  41. #define UNUSED __attribute__((unused))
  42. #else
  43. #define UNUSED
  44. #endif
  45. """)
  46. f.write('static const char UNUSED *ecoin_strings[] = {')
  47. for (msgid, msgstr) in messages:
  48. if msgid != EMPTY:
  49. f.write('QT_TRANSLATE_NOOP("ecoin-core", %s),\n' % ('\n'.join(msgid)))
  50. f.write('};')
  51. f.close()