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: Comments addressed Created Aug. 6, 2014, 10:31 a.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_locales = [
11 "ar",
12 "bg",
13 "ca",
14 "cs",
15 "da",
16 "de",
17 "el",
18 "en",
19 "es-ES",
20 "et",
21 "fi",
22 "fil",
23 "fr",
24 "he",
25 "hi",
26 "hr",
27 "hu",
28 "it",
29 "ja",
30 "kn",
31 "mr",
32 "ms",
33 "nb",
34 "nl",
35 "nn-NO",
36 "pl",
37 "pt-BR",
38 "pt-PT",
39 "ro",
40 "ru",
41 "sk",
42 "sv-SE",
43 "th",
44 "tr",
45 "uk",
46 "ur-PK",
47 "zh-CN",
48 "zh-TW"
49 ]
50
51 gecko_locale_mapping = {
52 "en": "en-US",
53 "hi": "hi-IN",
54 "nb": "nb-NO"
55 }
56
57 gecko_locale_base_path = "libadblockplus/adblockplus/chrome/locale"
58
59 strings_to_import = {
60 "firstRun.properties/firstRun_acceptableAdsHeadline": "first-run/first-run-aa- title",
61 "firstRun.properties/firstRun_acceptableAdsExplanation": "first-run/first-run- aa-text",
62 "filters.dtd/acceptableAds2.label": "settings/settings-acceptable-ads"
63 }
64
65 def read_gecko_locale_strings(locale):
66 locale_strings = {}
67 locale_files = set([key.split("/")[0] for key in strings_to_import.keys()])
68 for locale_file in locale_files:
69 locale_file_path = os.path.join(gecko_locale_base_path, locale, locale_file)
70 if os.path.exists(locale_file_path):
71 locale_strings[locale_file] = localeTools.readFile(locale_file_path)
72 else:
73 locale_strings[locale_file] = {}
74 return locale_strings
75
76 # This is to keep the locale file format largely intact -
77 # SafeConfigParser.write() puts spaces around equal signs.
78 def write_ini(config, file):
79 for index, section in enumerate(config.sections()):
80 if index > 0:
81 file.write("\n")
82 file.write("[%s]\n" % section)
83 items = config.items(section)
84 for key, value in items:
85 file.write("%s=%s\n" % (key, re.sub(r"\s+", " ", value, flags=re.S)))
86
87 def import_locale(ie_locale):
88 gecko_locale = gecko_locale_mapping.get(ie_locale, ie_locale)
89 gecko_locale_strings = read_gecko_locale_strings(gecko_locale)
90
91 ie_locale_path = "locales/%s.ini" % ie_locale
92 config = SafeConfigParser()
93 config.optionxform = str
94 with codecs.open(ie_locale_path, "r", "utf-8") as ie_locale_file:
95 config.readfp(ie_locale_file)
96
97 for source, target in strings_to_import.iteritems():
98 source_section, source_key = source.split("/")
99 target_section, target_key = target.split("/")
100 if source_key in gecko_locale_strings[source_section]:
101 value = gecko_locale_strings[source_section][source_key]
102 value = re.sub(r"\s*\(&.\)$", "", value).replace("&", "")
103 config.set(target_section, target_key, value)
104
105 with codecs.open(ie_locale_path, "w", "utf-8") as ie_locale_file:
106 write_ini(config, ie_locale_file)
107
108 def import_locales():
109 for ie_locale in ie_locales:
110 import_locale(ie_locale)
111
112 if __name__ == "__main__":
113 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