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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # help entries
  51. help_strings = []
  52. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  53. for fname in files:
  54. filepath = subdir + os.sep + fname
  55. if fname == 'help.go':
  56. content = open(filepath, 'r', encoding='UTF-8').read()
  57. matches = re.findall(r'\`([^\`]+)\`', content)
  58. for match in matches:
  59. if '\n' in match and match not in help_strings:
  60. help_strings.append(match)
  61. print("help strings:", len(help_strings))
  62. with open(os.path.join(arguments['<languages-dir>'], 'example-help.lang.json'), 'w') as f:
  63. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  64. f.write('\n')
  65. # nickserv help entries
  66. help_strings = []
  67. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  68. for fname in files:
  69. filepath = subdir + os.sep + fname
  70. if fname == 'nickserv.go':
  71. content = open(filepath, 'r', encoding='UTF-8').read()
  72. matches = re.findall(r'\`([^\`]+)\`', content)
  73. for match in matches:
  74. if '\n' in match and match not in help_strings:
  75. help_strings.append(match)
  76. print("nickserv help strings:", len(help_strings))
  77. with open(os.path.join(arguments['<languages-dir>'], 'example-nickserv.lang.json'), 'w') as f:
  78. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  79. f.write('\n')
  80. # chanserv help entries
  81. help_strings = []
  82. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  83. for fname in files:
  84. filepath = subdir + os.sep + fname
  85. if fname == 'chanserv.go':
  86. content = open(filepath, 'r', encoding='UTF-8').read()
  87. matches = re.findall(r'\`([^\`]+)\`', content)
  88. for match in matches:
  89. if '\n' in match and match not in help_strings:
  90. help_strings.append(match)
  91. print("chanserv help strings:", len(help_strings))
  92. with open(os.path.join(arguments['<languages-dir>'], 'example-chanserv.lang.json'), 'w') as f:
  93. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  94. f.write('\n')
  95. # hostserv help entries
  96. help_strings = []
  97. for subdir, dirs, files in os.walk(arguments['<irc-dir>']):
  98. for fname in files:
  99. filepath = subdir + os.sep + fname
  100. if fname == 'hostserv.go':
  101. content = open(filepath, 'r', encoding='UTF-8').read()
  102. matches = re.findall(r'\`([^\`]+)\`', content)
  103. for match in matches:
  104. if '\n' in match and match not in help_strings:
  105. help_strings.append(match)
  106. print("hostserv help strings:", len(help_strings))
  107. with open(os.path.join(arguments['<languages-dir>'], 'example-hostserv.lang.json'), 'w') as f:
  108. f.write(json.dumps({k:k for k in help_strings}, sort_keys=True, indent=2, separators=(',', ': ')))
  109. f.write('\n')