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.

100 lines
2.9 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. for alpha2 in sorted(os.listdir(source_locale_dir)):
  22. locale_path = pj(source_locale_dir, alpha2, 'messages.json')
  23. with open(locale_path, encoding='utf-8') as f:
  24. string_data = json.load(f, object_pairs_hook=OrderedDict)
  25. alpha2 = alpha2.replace('_', '-')
  26. descriptions[alpha2] = string_data['extShortDesc']['message']
  27. del string_data['extShortDesc']
  28. language_codes.append(alpha2)
  29. mkdirs(pj(target_locale_dir, alpha2))
  30. locale_path = pj(target_locale_dir, alpha2, 'messages.properties')
  31. with open(locale_path, 'wt', encoding='utf-8', newline='\n') as f:
  32. for string_name in string_data:
  33. f.write(string_name)
  34. f.write(u'=')
  35. f.write(string_data[string_name]['message'].replace('\n', r'\n'))
  36. f.write(u'\n')
  37. # generate chrome.manifest file
  38. chrome_manifest = pj(build_dir, 'chrome.manifest')
  39. with open(chrome_manifest, 'at', encoding='utf-8', newline='\n') as f:
  40. f.write(u'\nlocale ublock en ./locale/en/\n')
  41. for alpha2 in language_codes:
  42. if alpha2 == 'en':
  43. continue
  44. f.write(u'locale ublock ' + alpha2 + ' ./locale/' + alpha2 + '/\n')
  45. rmtree(source_locale_dir)
  46. # update install.rdf
  47. proj_dir = pj(os.path.split(os.path.abspath(__file__))[0], '..')
  48. chromium_manifest = pj(proj_dir, 'platform', 'chromium', 'manifest.json')
  49. with open(chromium_manifest, encoding='utf-8') as m:
  50. manifest = json.load(m)
  51. manifest['homepage'] = 'https://github.com/gorhill/uBlock'
  52. manifest['description'] = descriptions['en']
  53. del descriptions['en']
  54. manifest['localized'] = []
  55. t = ' '
  56. t3 = 3 * t
  57. for alpha2 in descriptions:
  58. if alpha2 == 'en':
  59. continue
  60. manifest['localized'].append(
  61. '\n' + t*2 + '<localized><r:Description>\n' +
  62. t3 + '<locale>' + alpha2 + '</locale>\n' +
  63. t3 + '<name>' + manifest['name'] + '</name>\n' +
  64. t3 + '<description>' + descriptions[alpha2] + '</description>\n' +
  65. t3 + '<creator>' + manifest['author'] + '</creator>\n' +
  66. # t3 + '<translator>' + ??? + '</translator>\n' +
  67. t3 + '<homepageURL>' + manifest['homepage'] + '</homepageURL>\n' +
  68. t*2 + '</r:Description></localized>'
  69. )
  70. manifest['localized'] = '\n'.join(manifest['localized'])
  71. install_rdf = pj(build_dir, 'install.rdf')
  72. with open(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
  73. install_rdf = f.read()
  74. f.seek(0)
  75. f.write(install_rdf.format(**manifest))