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