Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: import_locales.py

Issue 5748296123416576: Issue 1166 - Import strings from ABP for Firefox (Closed)
Left Patch Set: Created Aug. 5, 2014, 4:35 p.m.
Right Patch Set: Use localeTools.langMappingGecko, don't hard code IE locales Created Aug. 6, 2014, 12:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « .hgsubstate ('k') | locales/ar.ini » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import codecs 3 import codecs
4 from ConfigParser import ConfigParser 4 from ConfigParser import SafeConfigParser
Wladimir Palant 2014/08/06 06:09:04 We generally use SafeConfigParser
5 import os 5 import os
6 import re
6 7
7 from buildtools import localeTools 8 from buildtools import localeTools
8 9
9 ie_locales = [ 10 ie_locale_base_path = "locales"
10 "ar", 11 gecko_locale_base_path = "libadblockplus/adblockplus/chrome/locale"
11 "bg",
12 "ca",
13 "cs",
14 "da",
15 "de",
16 "el",
17 "en",
18 "es-ES",
19 "et",
20 "fi",
21 "fil",
22 "fr",
23 "he",
24 "hi",
25 "hr",
26 "hu",
27 "it",
28 "ja",
29 "kn",
30 "mr",
31 "ms",
32 "nb",
33 "nl",
34 "nn-NO",
35 "pl",
36 "pt-BR",
37 "pt-PT",
38 "ro",
39 "ru",
40 "sk",
41 "sv-SE",
42 "th",
43 "tr",
44 "uk",
45 "ur-PK",
46 "zh-CN",
47 "zh-TW"
48 ]
Felix Dahlke 2014/08/06 10:32:08 Yes, I just took the list of locales we currently
49 12
50 locale_mapping = { 13 gecko_locale_mapping = dict(zip(localeTools.langMappingGecko.values(),
51 "en": "en-US", 14 localeTools.langMappingGecko.keys()))
52 "hi": "hi-IN", 15
53 "nb": "nb-NO" 16 # We should be using en-US instead of en in IE, see
Felix Dahlke 2014/08/06 10:32:08 Here's a follow-up for that: https://issues.adbloc
54 } 17 # https://issues.adblockplus.org/ticket/1177
18 gecko_locale_mapping["en"] = "en-US"
55 19
56 strings_to_import = { 20 strings_to_import = {
57 "firstRun.properties/firstRun_acceptableAdsHeadline": "first-run/first-run-aa- title", 21 "firstRun.properties/firstRun_acceptableAdsHeadline": "first-run/first-run-aa- title",
58 "firstRun.properties/firstRun_acceptableAdsExplanation": "first-run/first-run- aa-text", 22 "firstRun.properties/firstRun_acceptableAdsExplanation": "first-run/first-run- aa-text",
59 "filters.dtd/acceptableAds2.label": "settings/settings-acceptable-ads" 23 "filters.dtd/acceptableAds2.label": "settings/settings-acceptable-ads"
60 } 24 }
61 25
62 def read_gecko_locale_strings(locale): 26 def read_gecko_locale_strings(locale):
63 locale_base_path = "libadblockplus/adblockplus/chrome/locale"
64 locale_files = ["firstRun.properties", "filters.dtd"]
65 locale_strings = {} 27 locale_strings = {}
28 locale_files = set([key.split("/")[0] for key in strings_to_import.keys()])
66 for locale_file in locale_files: 29 for locale_file in locale_files:
67 locale_file_path = "%s/%s/%s" % ( 30 locale_file_path = os.path.join(gecko_locale_base_path, locale, locale_file)
68 locale_base_path, locale, locale_file)
69 if os.path.exists(locale_file_path): 31 if os.path.exists(locale_file_path):
70 locale_strings[locale_file] = localeTools.readFile(locale_file_path) 32 locale_strings[locale_file] = localeTools.readFile(locale_file_path)
71 else: 33 else:
72 locale_strings[locale_file] = {} 34 locale_strings[locale_file] = {}
73 return locale_strings 35 return locale_strings
74 36
75 # This is to keep the locale file format largely intact - ConfigParser.write() 37 # This is to keep the locale file format largely intact -
76 # puts spaces around equal signs. 38 # SafeConfigParser.write() puts spaces around equal signs.
77 def write_ini(config, file): 39 def write_ini(config, file):
78 for index, section in enumerate(config.sections()): 40 for index, section in enumerate(config.sections()):
79 if index > 0: 41 if index > 0:
80 file.write("\n") 42 file.write("\n")
81 file.write("[%s]\n" % section) 43 file.write("[%s]\n" % section)
82 items = config.items(section) 44 items = config.items(section)
83 for item in items: 45 for key, value in items:
84 file.write("%s=%s\n" % item) 46 file.write("%s=%s\n" % (key, re.sub(r"\s+", " ", value, flags=re.S)))
Felix Dahlke 2014/08/06 10:32:08 Done.
85 47
86 def import_locale(ie_locale): 48 def import_locale(ie_locale):
87 if ie_locale in locale_mapping: 49 gecko_locale = gecko_locale_mapping.get(ie_locale, ie_locale)
88 gecko_locale = locale_mapping[ie_locale]
89 else:
90 gecko_locale = ie_locale
91 gecko_locale_strings = read_gecko_locale_strings(gecko_locale) 50 gecko_locale_strings = read_gecko_locale_strings(gecko_locale)
92 51
93 ie_locale_path = "locales/%s.ini" % ie_locale 52 ie_locale_path = "locales/%s.ini" % ie_locale
94 config = ConfigParser() 53 config = SafeConfigParser()
95 config.optionxform = str 54 config.optionxform = str
96 with codecs.open(ie_locale_path, "r", "utf-8") as ie_locale_file: 55 with codecs.open(ie_locale_path, "r", "utf-8") as ie_locale_file:
97 config.readfp(ie_locale_file) 56 config.readfp(ie_locale_file)
98 57
99 for source, target in strings_to_import.iteritems(): 58 for source, target in strings_to_import.iteritems():
100 source_section, source_key = source.split("/") 59 source_section, source_key = source.split("/")
101 target_section, target_key = target.split("/") 60 target_section, target_key = target.split("/")
102 if source_key in gecko_locale_strings[source_section]: 61 if source_key in gecko_locale_strings[source_section]:
103 value = gecko_locale_strings[source_section][source_key] 62 value = gecko_locale_strings[source_section][source_key]
104 value = value.replace("&", "") 63 value = re.sub(r"\s*\(&.\)$", "", value).replace("&", "")
Wladimir Palant 2014/08/06 06:11:04 This won't do the job for CJK locales. The strings
105 config.set(target_section, target_key, value) 64 config.set(target_section, target_key, value)
106 65
107 with codecs.open(ie_locale_path, "w", "utf-8") as ie_locale_file: 66 with codecs.open(ie_locale_path, "w", "utf-8") as ie_locale_file:
108 write_ini(config, ie_locale_file) 67 write_ini(config, ie_locale_file)
109 68
110 def import_locales(): 69 def import_locales():
70 ie_locales = [os.path.splitext(file)[0]
71 for file in os.listdir(ie_locale_base_path)
72 if os.path.isfile(os.path.join(ie_locale_base_path, file))]
111 for ie_locale in ie_locales: 73 for ie_locale in ie_locales:
112 import_locale(ie_locale) 74 import_locale(ie_locale)
113 75
114 if __name__ == "__main__": 76 if __name__ == "__main__":
115 import_locales() 77 import_locales()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld