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.

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