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

Delta Between Two Patch Sets: ext/common.js

Issue 29573083: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockplusui/
Left Patch Set: Created Oct. 10, 2017, 10:24 p.m.
Right Patch Set: Add polyfill.js to README.md Created Oct. 17, 2017, 12:35 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 | « devtools-panel.js ('k') | ext/content.js » ('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 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 }; 67 };
68 window.addEventListener("message", listener._extWrapper, false); 68 window.addEventListener("message", listener._extWrapper, false);
69 }, 69 },
70 70
71 removeListener(listener) 71 removeListener(listener)
72 { 72 {
73 if ("_extWrapper" in listener) 73 if ("_extWrapper" in listener)
74 window.removeEventListener("message", listener._extWrapper, false); 74 window.removeEventListener("message", listener._extWrapper, false);
75 } 75 }
76 }; 76 };
77
78 /* I18n */
79
80 let getLocaleCandidates = function(selectedLocale)
81 {
82 let candidates = [];
83 let defaultLocale = "en-US";
84
85 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second
86 // dash is dropped, since we only support language and region
87 let parts = selectedLocale.split("-");
88 let language = parts[0];
89 let region = (parts[1] || "").toUpperCase();
90
91 if (region)
92 candidates.push(language + "-" + region);
93
94 candidates.push(language);
95
96 if (candidates.indexOf(defaultLocale) == -1)
97 candidates.push(defaultLocale);
98
99 return candidates;
100 };
101
102 let selectedLocale = window.navigator.language;
103 let match = /[?&]locale=([\w-]+)/.exec(window.location.search);
104 if (match)
105 selectedLocale = match[1];
106
107 let locales = getLocaleCandidates(selectedLocale);
108 let catalog = Object.create(null);
109 let catalogFile = window.location.pathname.replace(/.*\//, "")
110 .replace(/\..*/, "") + ".json";
111
112 let replacePlaceholder = function(text, placeholder, content)
113 {
114 return text.split("$" + placeholder + "$").join(content || "");
115 };
116
117 let parseMessage = function(rawMessage)
118 {
119 let text = rawMessage.message;
120 let placeholders = [];
121
122 for (let placeholder in rawMessage.placeholders)
123 {
124 let {content} = rawMessage.placeholders[placeholder];
125
126 if (/^\$\d+$/.test(content))
127 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
128 else
129 text = replacePlaceholder(text, placeholder, content);
130 }
131
132 return [text, placeholders];
133 };
134
135 let readCatalog = function(locale, file)
136 {
137 let xhr = new XMLHttpRequest();
138 xhr.open("GET", "locale/" + locale + "/" + file, false);
139 xhr.overrideMimeType("text/plain");
140
141 try
142 {
143 xhr.send();
144 }
145 catch (e)
146 {
147 return;
148 }
149
150 if (xhr.status != 200 && xhr.status != 0)
151 return;
152
153 let rawCatalog = JSON.parse(xhr.responseText);
154 for (let msgId in rawCatalog)
155 {
156 if (!(msgId in catalog))
157 catalog[msgId] = parseMessage(rawCatalog[msgId]);
158 }
159 };
160
161 browser.i18n = {
Sebastian Noack 2017/10/10 23:23:08 I think this code should be moved to polyfill.js n
Manish Jethani 2017/10/11 11:29:06 Done.
162 getUILanguage()
163 {
164 return locales[0].replace(/_/g, "-");
165 },
166 getMessage(msgId, substitutions)
167 {
168 while (true)
169 {
170 let message = catalog[msgId];
171 if (message)
172 {
173 let text = message[0];
174 let placeholders = message[1];
175
176 if (!(substitutions instanceof Array))
177 substitutions = [substitutions];
178
179 for (let i = 0; i < placeholders.length; i++)
180 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
181
182 return text;
183 }
184
185 if (locales.length == 0)
186 return "";
187
188 let locale = locales.shift();
189 readCatalog(locale, "common.json");
190 readCatalog(locale, catalogFile);
191 }
192 }
193 };
194 }()); 77 }());
LEFTRIGHT

Powered by Google App Engine
This is Rietveld