OLD | NEW |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 (function() | 20 (function() |
21 { | 21 { |
22 if (typeof chrome == "undefined") | |
23 window.chrome = {}; | |
24 | |
25 if (typeof ext == "undefined") | 22 if (typeof ext == "undefined") |
26 window.ext = {}; | 23 window.ext = {}; |
27 | 24 |
28 function Page(source) | 25 function Page(source) |
29 { | 26 { |
30 this._source = source; | 27 this._source = source; |
31 } | 28 } |
32 Page.prototype = | 29 Page.prototype = |
33 { | 30 { |
34 sendMessage(message) | 31 sendMessage(message) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 }; | 67 }; |
71 window.addEventListener("message", listener._extWrapper, false); | 68 window.addEventListener("message", listener._extWrapper, false); |
72 }, | 69 }, |
73 | 70 |
74 removeListener(listener) | 71 removeListener(listener) |
75 { | 72 { |
76 if ("_extWrapper" in listener) | 73 if ("_extWrapper" in listener) |
77 window.removeEventListener("message", listener._extWrapper, false); | 74 window.removeEventListener("message", listener._extWrapper, false); |
78 } | 75 } |
79 }; | 76 }; |
80 | |
81 /* I18n */ | |
82 | |
83 let getLocaleCandidates = function(selectedLocale) | |
84 { | |
85 let candidates = []; | |
86 let defaultLocale = "en-US"; | |
87 | |
88 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | |
89 // dash is dropped, since we only support language and region | |
90 let parts = selectedLocale.split("-"); | |
91 let language = parts[0]; | |
92 let region = (parts[1] || "").toUpperCase(); | |
93 | |
94 if (region) | |
95 candidates.push(language + "-" + region); | |
96 | |
97 candidates.push(language); | |
98 | |
99 if (candidates.indexOf(defaultLocale) == -1) | |
100 candidates.push(defaultLocale); | |
101 | |
102 return candidates; | |
103 }; | |
104 | |
105 let selectedLocale = window.navigator.language; | |
106 let match = /[?&]locale=([\w-]+)/.exec(window.location.search); | |
107 if (match) | |
108 selectedLocale = match[1]; | |
109 | |
110 let locales = getLocaleCandidates(selectedLocale); | |
111 let catalog = Object.create(null); | |
112 let catalogFile = window.location.pathname.replace(/.*\//, "") | |
113 .replace(/\..*/, "") + ".json"; | |
114 | |
115 let replacePlaceholder = function(text, placeholder, content) | |
116 { | |
117 return text.split("$" + placeholder + "$").join(content || ""); | |
118 }; | |
119 | |
120 let parseMessage = function(rawMessage) | |
121 { | |
122 let text = rawMessage.message; | |
123 let placeholders = []; | |
124 | |
125 for (let placeholder in rawMessage.placeholders) | |
126 { | |
127 let {content} = rawMessage.placeholders[placeholder]; | |
128 | |
129 if (/^\$\d+$/.test(content)) | |
130 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; | |
131 else | |
132 text = replacePlaceholder(text, placeholder, content); | |
133 } | |
134 | |
135 return [text, placeholders]; | |
136 }; | |
137 | |
138 let readCatalog = function(locale, file) | |
139 { | |
140 let xhr = new XMLHttpRequest(); | |
141 xhr.open("GET", "locale/" + locale + "/" + file, false); | |
142 xhr.overrideMimeType("text/plain"); | |
143 | |
144 try | |
145 { | |
146 xhr.send(); | |
147 } | |
148 catch (e) | |
149 { | |
150 return; | |
151 } | |
152 | |
153 if (xhr.status != 200 && xhr.status != 0) | |
154 return; | |
155 | |
156 let rawCatalog = JSON.parse(xhr.responseText); | |
157 for (let msgId in rawCatalog) | |
158 { | |
159 if (!(msgId in catalog)) | |
160 catalog[msgId] = parseMessage(rawCatalog[msgId]); | |
161 } | |
162 }; | |
163 | |
164 chrome.i18n = { | |
165 getUILanguage() | |
166 { | |
167 return locales[0].replace(/_/g, "-"); | |
168 }, | |
169 getMessage(msgId, substitutions) | |
170 { | |
171 while (true) | |
172 { | |
173 let message = catalog[msgId]; | |
174 if (message) | |
175 { | |
176 let text = message[0]; | |
177 let placeholders = message[1]; | |
178 | |
179 if (!(substitutions instanceof Array)) | |
180 substitutions = [substitutions]; | |
181 | |
182 for (let i = 0; i < placeholders.length; i++) | |
183 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | |
184 | |
185 return text; | |
186 } | |
187 | |
188 if (locales.length == 0) | |
189 return ""; | |
190 | |
191 let locale = locales.shift(); | |
192 readCatalog(locale, "common.json"); | |
193 readCatalog(locale, catalogFile); | |
194 } | |
195 } | |
196 }; | |
197 }()); | 77 }()); |
OLD | NEW |