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.

77 lines
2.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. description = ''
  21. for alpha2 in 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. if alpha2 == 'en':
  26. description = string_data['extShortDesc']['message']
  27. alpha2 = alpha2.replace('_', '-')
  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('=')
  35. f.write(string_data[string_name]['message'].replace('\n', r'\n'))
  36. f.write('\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('\nlocale ublock en ./locale/en/\n')
  41. for alpha2 in language_codes:
  42. if alpha2 != 'en':
  43. f.write('locale ublock ' + alpha2 + ' ./locale/' + alpha2 + '/\n')
  44. rmtree(source_locale_dir)
  45. # update install.rdf
  46. proj_dir = pj(os.path.split(os.path.abspath(__file__))[0], '..')
  47. chromium_manifest = pj(proj_dir, 'platform', 'chromium', 'manifest.json')
  48. with open(chromium_manifest, encoding='utf-8') as m:
  49. manifest = json.load(m)
  50. manifest['description'] = description
  51. install_rdf = pj(build_dir, 'install.rdf')
  52. with open(install_rdf, 'r+t', encoding='utf-8', newline='\n') as f:
  53. install_rdf = f.read()
  54. f.seek(0)
  55. f.write(install_rdf.format(**manifest))