OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # coding: utf-8 |
| 3 # |
| 4 # This Source Code is subject to the terms of the Mozilla Public License |
| 5 # version 2.0 (the "License"). You can obtain a copy of the License at |
| 6 # http://mozilla.org/MPL/2.0/. |
| 7 |
| 8 """Copies the locales from ABP for Chrome and prepares them for Opera""" |
| 9 |
| 10 import os, shutil, sys |
| 11 |
| 12 def remove(path): |
| 13 if os.path.exists(path): |
| 14 if os.path.isdir(path): |
| 15 shutil.rmtree(path) |
| 16 else: |
| 17 os.remove(path) |
| 18 |
| 19 def map_locale(locale): |
| 20 locale = locale.lower().replace("_", "-") |
| 21 |
| 22 mapping = { |
| 23 "en-us": "en", |
| 24 "es": "es-es", |
| 25 "es-419": "es-la", |
| 26 "pt-pt": "pt" |
| 27 } |
| 28 |
| 29 if locale in mapping: |
| 30 return mapping[locale] |
| 31 |
| 32 return locale |
| 33 |
| 34 def copy_messages(source, destination): |
| 35 messages_file_name = "messages.json" |
| 36 |
| 37 source_path = os.path.join(source, messages_file_name) |
| 38 source_file = open(source_path) |
| 39 messages = source_file.read() |
| 40 source_file.close() |
| 41 |
| 42 messages = messages.replace("Chrome", "Opera") |
| 43 |
| 44 dest_path = os.path.join(destination, messages_file_name) |
| 45 dest_file = open(dest_path, "w+") |
| 46 dest_file.write(messages) |
| 47 dest_file.close() |
| 48 |
| 49 def write_locale(target_dir, locale): |
| 50 # We have to write the actual locale to a localised file because Opera doesn't |
| 51 # show the country part in navigator.language (only "en", not "en-gb"). |
| 52 locale_file = open(os.path.join(target_dir, "locale.js"), "w+"); |
| 53 locale_file.write('window.locale = "%s";\n' % locale); |
| 54 locale_file.close(); |
| 55 |
| 56 def copy_locales(source, destination): |
| 57 for source_locale in os.listdir(source): |
| 58 source_locale_path = os.path.join(source, source_locale) |
| 59 if not os.path.isdir(source_locale_path): |
| 60 continue |
| 61 |
| 62 dest_locale = map_locale(source_locale) |
| 63 dest_locale_path = os.path.join(destination, dest_locale) |
| 64 |
| 65 os.mkdir(dest_locale_path) |
| 66 copy_messages(source_locale_path, dest_locale_path) |
| 67 write_locale(dest_locale_path, dest_locale) |
| 68 |
| 69 def update_locales(): |
| 70 locales_dir = "locales" |
| 71 remove(locales_dir) |
| 72 os.mkdir(locales_dir) |
| 73 |
| 74 chrome_locales_dir = os.path.join("..", "adblockpluschrome", "_locales") |
| 75 if not os.path.exists(chrome_locales_dir): |
| 76 message = "Unable to find Chrome locales in %s" % chrome_locales_dir |
| 77 print >>sys.stderr, message |
| 78 |
| 79 copy_locales(chrome_locales_dir, locales_dir) |
| 80 |
| 81 if __name__ == "__main__": |
| 82 update_locales() |
OLD | NEW |