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

Side by Side Diff: import_locales.py

Issue 5748296123416576: Issue 1166 - Import strings from ABP for Firefox (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « .hgsubstate ('k') | locales/ar.ini » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 import codecs
4 from ConfigParser import SafeConfigParser
5 import os
6 import re
7
8 from buildtools import localeTools
9
10 ie_locale_base_path = "locales"
11 gecko_locale_base_path = "libadblockplus/adblockplus/chrome/locale"
12
13 gecko_locale_mapping = dict(zip(localeTools.langMappingGecko.values(),
14 localeTools.langMappingGecko.keys()))
15
16 # We should be using en-US instead of en in IE, see
17 # https://issues.adblockplus.org/ticket/1177
18 gecko_locale_mapping["en"] = "en-US"
19
20 strings_to_import = {
21 "firstRun.properties/firstRun_acceptableAdsHeadline": "first-run/first-run-aa- title",
22 "firstRun.properties/firstRun_acceptableAdsExplanation": "first-run/first-run- aa-text",
23 "filters.dtd/acceptableAds2.label": "settings/settings-acceptable-ads"
24 }
25
26 def read_gecko_locale_strings(locale):
27 locale_strings = {}
28 locale_files = set([key.split("/")[0] for key in strings_to_import.keys()])
29 for locale_file in locale_files:
30 locale_file_path = os.path.join(gecko_locale_base_path, locale, locale_file)
31 if os.path.exists(locale_file_path):
32 locale_strings[locale_file] = localeTools.readFile(locale_file_path)
33 else:
34 locale_strings[locale_file] = {}
35 return locale_strings
36
37 # This is to keep the locale file format largely intact -
38 # SafeConfigParser.write() puts spaces around equal signs.
39 def write_ini(config, file):
40 for index, section in enumerate(config.sections()):
41 if index > 0:
42 file.write("\n")
43 file.write("[%s]\n" % section)
44 items = config.items(section)
45 for key, value in items:
46 file.write("%s=%s\n" % (key, re.sub(r"\s+", " ", value, flags=re.S)))
47
48 def import_locale(ie_locale):
49 gecko_locale = gecko_locale_mapping.get(ie_locale, ie_locale)
50 gecko_locale_strings = read_gecko_locale_strings(gecko_locale)
51
52 ie_locale_path = "locales/%s.ini" % ie_locale
53 config = SafeConfigParser()
54 config.optionxform = str
55 with codecs.open(ie_locale_path, "r", "utf-8") as ie_locale_file:
56 config.readfp(ie_locale_file)
57
58 for source, target in strings_to_import.iteritems():
59 source_section, source_key = source.split("/")
60 target_section, target_key = target.split("/")
61 if source_key in gecko_locale_strings[source_section]:
62 value = gecko_locale_strings[source_section][source_key]
63 value = re.sub(r"\s*\(&.\)$", "", value).replace("&", "")
64 config.set(target_section, target_key, value)
65
66 with codecs.open(ie_locale_path, "w", "utf-8") as ie_locale_file:
67 write_ini(config, ie_locale_file)
68
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))]
73 for ie_locale in ie_locales:
74 import_locale(ie_locale)
75
76 if __name__ == "__main__":
77 import_locales()
OLDNEW
« no previous file with comments | « .hgsubstate ('k') | locales/ar.ini » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld