Left: | ||
Right: |
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 return locale.lower().replace("_", "-") | 20 locale = locale.replace("_", "-") |
Wladimir Palant
2012/10/18 12:30:40
Actually, the correct mapping is a bit more compli
Felix Dahlke
2012/10/18 13:09:11
Done.
| |
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 | |
21 | 33 |
22 def copy_messages(source, destination): | 34 def copy_messages(source, destination): |
23 messages_file_name = "messages.json" | 35 messages_file_name = "messages.json" |
24 | 36 |
25 source_path = os.path.join(source, messages_file_name) | 37 source_path = os.path.join(source, messages_file_name) |
26 source_file = open(source_path) | 38 source_file = open(source_path) |
27 messages = source_file.read() | 39 messages = source_file.read() |
28 source_file.close() | 40 source_file.close() |
29 | 41 |
30 messages = messages.replace("Chrome", "Opera") | 42 messages = messages.replace("Chrome", "Opera") |
Wladimir Palant
2012/10/18 12:30:40
LOL
Let's hope this doesn't have any unintended si
Felix Dahlke
2012/10/18 13:09:11
Right now it doesn't :) Maybe r"\bChrome\b" to be
| |
31 | 43 |
32 dest_path = os.path.join(destination, messages_file_name) | 44 dest_path = os.path.join(destination, messages_file_name) |
33 dest_file = open(dest_path, "w+") | 45 dest_file = open(dest_path, "w+") |
34 dest_file.write(messages) | 46 dest_file.write(messages) |
35 dest_file.close() | 47 dest_file.close() |
36 | 48 |
37 def write_locale(target_dir, locale): | |
38 # We have to write the actual locale to a localised file because Opera doesn't | |
39 # show the country part in navigator.language (only "de", not "de-de"). | |
Wladimir Palant
2012/10/18 12:30:40
That isn't really the case. navigator.lenguage ret
Felix Dahlke
2012/10/18 13:09:11
I'm definitely using en-GB and navigator.language
| |
40 locale_file = open(os.path.join(target_dir, "locale.js"), "w+"); | |
41 locale_file.write('window.locale = "%s";\n' % locale); | |
42 locale_file.close(); | |
43 | |
44 def copy_locales(source, destination): | 49 def copy_locales(source, destination): |
45 for source_locale in os.listdir(source): | 50 for source_locale in os.listdir(source): |
46 source_locale_path = os.path.join(source, source_locale) | 51 source_locale_path = os.path.join(source, source_locale) |
47 if not os.path.isdir(source_locale_path): | 52 if not os.path.isdir(source_locale_path): |
48 continue | 53 continue |
49 | 54 |
50 dest_locale = map_locale(source_locale) | 55 dest_locale = map_locale(source_locale) |
51 dest_locale_path = os.path.join(destination, dest_locale) | 56 dest_locale_path = os.path.join(destination, dest_locale) |
52 | 57 |
53 os.mkdir(dest_locale_path) | 58 os.mkdir(dest_locale_path) |
54 copy_messages(source_locale_path, dest_locale_path) | 59 copy_messages(source_locale_path, dest_locale_path) |
55 write_locale(dest_locale_path, dest_locale) | |
56 | 60 |
57 def update_locales(): | 61 def update_locales(): |
58 locales_dir = "locales" | 62 locales_dir = "locales" |
59 remove(locales_dir) | 63 remove(locales_dir) |
60 os.mkdir(locales_dir) | 64 os.mkdir(locales_dir) |
61 | 65 |
62 chrome_locales_dir = os.path.join("..", "adblockpluschrome", "_locales") | 66 chrome_locales_dir = os.path.join("..", "adblockpluschrome", "_locales") |
63 if not os.path.exists(chrome_locales_dir): | 67 if not os.path.exists(chrome_locales_dir): |
64 message = "Unable to find Chrome locales in %s" % chrome_locales_dir | 68 message = "Unable to find Chrome locales in %s" % chrome_locales_dir |
65 print >>sys.stderr, message | 69 print >>sys.stderr, message |
66 | 70 |
67 copy_locales(chrome_locales_dir, locales_dir) | 71 copy_locales(chrome_locales_dir, locales_dir) |
68 | 72 |
69 if __name__ == "__main__": | 73 if __name__ == "__main__": |
70 update_locales() | 74 update_locales() |
LEFT | RIGHT |