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: Created Aug. 5, 2014, 4:35 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 ConfigParser
Wladimir Palant 2014/08/06 06:09:04 We generally use SafeConfigParser
5 import os
6
7 from buildtools import localeTools
8
9 ie_locales = [
10 "ar",
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
50 locale_mapping = {
51 "en": "en-US",
52 "hi": "hi-IN",
53 "nb": "nb-NO"
Felix Dahlke 2014/08/06 10:32:08 Here's a follow-up for that: https://issues.adbloc
54 }
55
56 strings_to_import = {
57 "firstRun.properties/firstRun_acceptableAdsHeadline": "first-run/first-run-aa- title",
58 "firstRun.properties/firstRun_acceptableAdsExplanation": "first-run/first-run- aa-text",
59 "filters.dtd/acceptableAds2.label": "settings/settings-acceptable-ads"
60 }
61
62 def read_gecko_locale_strings(locale):
63 locale_base_path = "libadblockplus/adblockplus/chrome/locale"
64 locale_files = ["firstRun.properties", "filters.dtd"]
65 locale_strings = {}
66 for locale_file in locale_files:
67 locale_file_path = "%s/%s/%s" % (
68 locale_base_path, locale, locale_file)
69 if os.path.exists(locale_file_path):
70 locale_strings[locale_file] = localeTools.readFile(locale_file_path)
71 else:
72 locale_strings[locale_file] = {}
73 return locale_strings
74
75 # This is to keep the locale file format largely intact - ConfigParser.write()
76 # puts spaces around equal signs.
77 def write_ini(config, file):
78 for index, section in enumerate(config.sections()):
79 if index > 0:
80 file.write("\n")
81 file.write("[%s]\n" % section)
82 items = config.items(section)
83 for item in items:
84 file.write("%s=%s\n" % item)
Felix Dahlke 2014/08/06 10:32:08 Done.
85
86 def import_locale(ie_locale):
87 if ie_locale in locale_mapping:
88 gecko_locale = locale_mapping[ie_locale]
89 else:
90 gecko_locale = ie_locale
91 gecko_locale_strings = read_gecko_locale_strings(gecko_locale)
92
93 ie_locale_path = "locales/%s.ini" % ie_locale
94 config = ConfigParser()
95 config.optionxform = str
96 with codecs.open(ie_locale_path, "r", "utf-8") as ie_locale_file:
97 config.readfp(ie_locale_file)
98
99 for source, target in strings_to_import.iteritems():
100 source_section, source_key = source.split("/")
101 target_section, target_key = target.split("/")
102 if source_key in gecko_locale_strings[source_section]:
103 value = gecko_locale_strings[source_section][source_key]
104 value = 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)
106
107 with codecs.open(ie_locale_path, "w", "utf-8") as ie_locale_file:
108 write_ini(config, ie_locale_file)
109
110 def import_locales():
111 for ie_locale in ie_locales:
112 import_locale(ie_locale)
113
114 if __name__ == "__main__":
115 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