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.

107 lines
3.1 KiB

  1. #!/usr/bin/env python3
  2. import os
  3. import json
  4. import sys
  5. from io import open
  6. from shutil import rmtree
  7. from collections import OrderedDict
  8. from xml.sax.saxutils import escape
  9. if len(sys.argv) == 1 or not sys.argv[1]:
  10. raise SystemExit('Build dir missing.')
  11. def mkdirs(path):
  12. try:
  13. os.makedirs(path)
  14. finally:
  15. return os.path.exists(path)
  16. pj = os.path.join
  17. build_dir = os.path.abspath(sys.argv[1])
  18. source_locale_dir = pj(build_dir, '_locales')
  19. target_locale_dir = pj(build_dir, 'locale')
  20. language_codes = []
  21. descriptions = OrderedDict({})
  22. title_case_strings = ['pickerContextMenuEntry']
  23. for alpha2 in sorted(os.listdir(source_locale_dir)):
  24. locale_path = pj(source_locale_dir, alpha2, 'messages.json')
  25. with open(locale_path, encoding='utf-8') as f:
  26. strings = json.load(f, object_pairs_hook=OrderedDict)
  27. alpha2 = alpha2.replace('_', '-')
  28. descriptions[alpha2] = strings['extShortDesc']['message']
  29. del strings['extShortDesc']
  30. language_codes.append(alpha2)
  31. mkdirs(pj(target_locale_dir, alpha2))
  32. locale_path = pj(target_locale_dir, alpha2, 'messages.properties')
  33. with open(locale_path, 'wt', encoding='utf-8', newline='\n') as f:
  34. for string_name in strings:
  35. string = strings[string_name]['message']
  36. if alpha2 == 'en' and string_name in title_case_strings:
  37. string = string.title()
  38. f.write(string_name)
  39. f.write(u'=')
  40. f.write(string.replace('\n', r'\n'))
  41. f.write(u'\n')
  42. # generate chrome.manifest file
  43. chrome_manifest = pj(build_dir, 'chrome.manifest')
  44. with open(chrome_manifest, 'at', encoding='utf-8', newline='\n') as f:
  45. f.write(u'\nlocale umatrix en ./locale/en/\n')
  46. for alpha2 in language_codes:
  47. if alpha2 == 'en':
  48. continue
  49. f.write(u'locale umatrix ' + alpha2 + ' ./locale/' + alpha2 + '/\n')
  50. rmtree(source_locale_dir)
  51. # update install.rdf
  52. proj_dir = pj(os.path.split(os.path.abspath(__file__))[0], '..')
  53. chromium_manifest = pj(proj_dir, 'platform', 'chromium', 'manifest.json')
  54. with open(chromium_manifest, encoding='utf-8') as m:
  55. manifest = json.load(m)
  56. manifest['homepage'] = 'https://github.com/gorhill/uMatrix'
  57. manifest['description'] = escape(descriptions['en'])
  58. del descriptions['en']
  59. manifest['localized'] = []
  60. t = ' '
  61. t3 = 3 * t
  62. for alpha2 in descriptions:
  63. if alpha2 == 'en':
  64. continue
  65. manifest['localized'].append(
  66. '\n' + t*2 + '<localized><r:Description>\n' +
  67. t3 + '<locale>' + alpha2 + '</locale>\n' +
  68. t3 + '<name>' + manifest['name'] + '</name>\n' +
  69. t3 + '<description>' + escape(descriptions[alpha2]) + '</description>\n' +
  70. t3 + '<creator>' + manifest['author'] + '</creator>\n' +
  71. # t3 + '<translator>' + ??? + '</translator>\n' +
  72. t3 + '<homepageURL>' + manifest['homepage'] + '</homepageURL>\n' +
  73. t*2 + '</r:Description></localized>'
  74. )
  75. manifest['localized'] = '\n'.join(manifest['localized'])
  76. install_rdf = pj(build_dir, 'install.rdf')
  77. with open(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
  78. install_rdf = f.read()
  79. f.seek(0)
  80. f.write(install_rdf.format(**manifest))