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.

updatetranslations.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. # updatetranslations.py
  3. #
  4. # tl;dr this script updates our translation file with the newest, coolest strings we've added!
  5. # it manually searches the source code, extracts strings and then updates the language files.
  6. # Written in 2018 by Daniel Oaks <daniel@danieloaks.net>
  7. #
  8. # To the extent possible under law, the author(s) have dedicated all copyright
  9. # and related and neighboring rights to this software to the public domain
  10. # worldwide. This software is distributed without any warranty.
  11. #
  12. # You should have received a copy of the CC0 Public Domain Dedication along
  13. # with this software. If not, see
  14. # <http://creativecommons.org/publicdomain/zero/1.0/>.
  15. """updatetranslations.py
  16. Usage:
  17. updatetranslations.py run <irc-dir> <languages-dir>
  18. updatetranslations.py --version
  19. updatetranslations.py (-h | --help)
  20. Options:
  21. <irc-dir> Oragono's irc subdirectory where the Go code is kept.
  22. <languages-dir> Languages directory."""
  23. import os
  24. import re
  25. import json
  26. from docopt import docopt
  27. import yaml
  28. if __name__ == '__main__':
  29. arguments = docopt(__doc__, version="0.1.0")
  30. if arguments['run']:
  31. # general IRC strings
  32. irc_strings = []
  33. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  34. for fname in files:
  35. filepath = subdir + os.sep + fname
  36. if filepath.endswith('.go'):
  37. content = open(filepath, 'r', encoding='UTF-8').read()
  38. matches = re.findall(r'\.t\("((?:[^"]|\\")+)"\)', content)
  39. for match in matches:
  40. if match not in irc_strings:
  41. irc_strings.append(match)
  42. matches = re.findall(r'\.t\(\`([^\`]+)\`\)', content)
  43. for match in matches:
  44. if match not in irc_strings:
  45. irc_strings.append(match)
  46. print("irc strings:", len(irc_strings))
  47. with open(os.path.join(arguments['<languages-dir>'], 'example', 'irc.lang.json'), 'w') as f:
  48. f.write(json.dumps({k:k for k in irc_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  49. f.write('\n')
  50. for string in irc_strings:
  51. if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
  52. print(' confirm:', string)
  53. # help entries
  54. help_strings = []
  55. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  56. for fname in files:
  57. filepath = subdir + os.sep + fname
  58. if fname == 'help.go':
  59. content = open(filepath, 'r', encoding='UTF-8').read()
  60. matches = re.findall(r'\`([^\`]+)\`', content)
  61. for match in matches:
  62. if '\n' in match and match not in help_strings:
  63. help_strings.append(match)
  64. print("help strings:", len(help_strings))
  65. with open(os.path.join(arguments['<languages-dir>'], 'example', 'help.lang.json'), 'w') as f:
  66. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  67. f.write('\n')
  68. for string in help_strings:
  69. if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
  70. print(' confirm:', string.split('\n')[0])
  71. # nickserv help entries
  72. help_strings = []
  73. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  74. for fname in files:
  75. filepath = subdir + os.sep + fname
  76. if fname == 'nickserv.go':
  77. content = open(filepath, 'r', encoding='UTF-8').read()
  78. matches = re.findall(r'\`([^\`]+)\`', content)
  79. for match in matches:
  80. if match not in help_strings:
  81. help_strings.append(match)
  82. print("nickserv help strings:", len(help_strings))
  83. with open(os.path.join(arguments['<languages-dir>'], 'example', 'nickserv.lang.json'), 'w') as f:
  84. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  85. f.write('\n')
  86. for string in help_strings:
  87. if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
  88. print(' confirm:', string)
  89. # chanserv help entries
  90. help_strings = []
  91. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  92. for fname in files:
  93. filepath = subdir + os.sep + fname
  94. if fname == 'chanserv.go':
  95. content = open(filepath, 'r', encoding='UTF-8').read()
  96. matches = re.findall(r'\`([^\`]+)\`', content)
  97. for match in matches:
  98. if match not in help_strings:
  99. help_strings.append(match)
  100. print("chanserv help strings:", len(help_strings))
  101. with open(os.path.join(arguments['<languages-dir>'], 'example', 'chanserv.lang.json'), 'w') as f:
  102. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  103. f.write('\n')
  104. for string in help_strings:
  105. if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
  106. print(' confirm:', string)
  107. # hostserv help entries
  108. help_strings = []
  109. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  110. for fname in files:
  111. filepath = subdir + os.sep + fname
  112. if fname == 'hostserv.go':
  113. content = open(filepath, 'r', encoding='UTF-8').read()
  114. matches = re.findall(r'\`([^\`]+)\`', content)
  115. for match in matches:
  116. if match not in help_strings:
  117. help_strings.append(match)
  118. print("hostserv help strings:", len(help_strings))
  119. with open(os.path.join(arguments['<languages-dir>'], 'example', 'hostserv.lang.json'), 'w') as f:
  120. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  121. f.write('\n')
  122. for string in help_strings:
  123. if 1 < string.count('%s') + string.count('%d') + string.count('%f'):
  124. print(' confirm:', string)