LEFT | RIGHT |
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-2016 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 (function(global) | 18 "use strict"; |
| 19 |
| 20 (function() |
19 { | 21 { |
20 if (!global.ext) | 22 if (typeof ext == "undefined") |
21 global.ext = {}; | 23 window.ext = {}; |
22 | 24 |
23 function Page(source) | 25 function Page(source) |
24 { | 26 { |
25 this._source = source; | 27 this._source = source; |
26 } | 28 } |
27 Page.prototype = | 29 Page.prototype = |
28 { | 30 { |
29 sendMessage: function(message) | 31 sendMessage(message) |
30 { | 32 { |
31 this._source.postMessage({ | 33 this._source.postMessage({ |
32 type: "message", | 34 type: "message", |
33 messageId: -1, | 35 messageId: -1, |
34 payload: message | 36 payload: message |
35 }, "*"); | 37 }, "*"); |
36 } | 38 } |
37 }; | 39 }; |
38 | 40 |
39 global.ext.Page = Page; | 41 window.ext.Page = Page; |
40 | 42 |
41 /* Message passing */ | 43 /* Message passing */ |
42 | 44 |
43 global.ext.onMessage = | 45 window.ext.onMessage = |
44 { | 46 { |
45 addListener: function(listener) | 47 addListener(listener) |
46 { | 48 { |
47 listener._extWrapper = function(event) | 49 listener._extWrapper = function(event) |
48 { | 50 { |
49 if (event.data.type != "message") | 51 if (event.data.type != "message") |
50 return; | 52 return; |
51 | 53 |
52 var message = event.data.payload; | 54 let {messageId} = event.data; |
53 var messageId = event.data.messageId; | 55 let sender = { |
54 var sender = { | |
55 page: new Page(event.source) | 56 page: new Page(event.source) |
56 }; | 57 }; |
57 var callback = function(message) | 58 let callback = function(message) |
58 { | 59 { |
59 event.source.postMessage({ | 60 event.source.postMessage({ |
60 type: "response", | 61 type: "response", |
61 messageId: messageId, | 62 messageId, |
62 payload: message | 63 payload: message |
63 }, "*"); | 64 }, "*"); |
64 }; | 65 }; |
65 listener(message, sender, callback); | 66 listener(event.data.payload, sender, callback); |
66 }; | 67 }; |
67 window.addEventListener("message", listener._extWrapper, false); | 68 window.addEventListener("message", listener._extWrapper, false); |
68 }, | 69 }, |
69 | 70 |
70 removeListener: function(listener) | 71 removeListener(listener) |
71 { | 72 { |
72 if ("_extWrapper" in listener) | 73 if ("_extWrapper" in listener) |
73 window.removeEventListener("message", listener._extWrapper, false); | 74 window.removeEventListener("message", listener._extWrapper, false); |
74 } | 75 } |
75 }; | 76 }; |
76 | 77 |
77 /* I18n */ | 78 /* I18n */ |
78 | 79 |
79 var getLocaleCandidates = function(selectedLocale) | 80 let getLocaleCandidates = function(selectedLocale) |
80 { | 81 { |
81 var candidates = []; | 82 let candidates = []; |
82 var defaultLocale = "en-US"; | 83 let defaultLocale = "en-US"; |
83 | 84 |
84 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | 85 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second |
85 // dash is dropped, since we only support language and region | 86 // dash is dropped, since we only support language and region |
86 var parts = selectedLocale.split("-"); | 87 let parts = selectedLocale.split("-"); |
87 var language = parts[0]; | 88 let language = parts[0]; |
88 var region = (parts[1] || "").toUpperCase(); | 89 let region = (parts[1] || "").toUpperCase(); |
89 | 90 |
90 if (region) | 91 if (region) |
91 candidates.push(language + "-" + region); | 92 candidates.push(language + "-" + region); |
92 | 93 |
93 candidates.push(language); | 94 candidates.push(language); |
94 | 95 |
95 if (candidates.indexOf(defaultLocale) == -1) | 96 if (candidates.indexOf(defaultLocale) == -1) |
96 candidates.push(defaultLocale); | 97 candidates.push(defaultLocale); |
97 | 98 |
98 return candidates; | 99 return candidates; |
99 }; | 100 }; |
100 | 101 |
101 var selectedLocale = window.navigator.language; | 102 let selectedLocale = window.navigator.language; |
102 var match = /[?&]locale=([\w\-]+)/.exec(window.location.search); | 103 let match = /[?&]locale=([\w-]+)/.exec(window.location.search); |
103 if (match) | 104 if (match) |
104 selectedLocale = match[1]; | 105 selectedLocale = match[1]; |
105 | 106 |
106 var locales = getLocaleCandidates(selectedLocale); | 107 let locales = getLocaleCandidates(selectedLocale); |
107 var catalog = Object.create(null); | 108 let catalog = Object.create(null); |
108 var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/,
"") + ".json"; | 109 let catalogFile = window.location.pathname.replace(/.*\//, "") |
| 110 .replace(/\..*/, "") + ".json"; |
109 | 111 |
110 var replacePlaceholder = function(text, placeholder, content) | 112 let replacePlaceholder = function(text, placeholder, content) |
111 { | 113 { |
112 return text.split("$" + placeholder + "$").join(content || ""); | 114 return text.split("$" + placeholder + "$").join(content || ""); |
113 }; | 115 }; |
114 | 116 |
115 var parseMessage = function(rawMessage) | 117 let parseMessage = function(rawMessage) |
116 { | 118 { |
117 var text = rawMessage.message; | 119 let text = rawMessage.message; |
118 var placeholders = []; | 120 let placeholders = []; |
119 | 121 |
120 for (var placeholder in rawMessage.placeholders) | 122 for (let placeholder in rawMessage.placeholders) |
121 { | 123 { |
122 var content = rawMessage.placeholders[placeholder].content; | 124 let {content} = rawMessage.placeholders[placeholder]; |
123 | 125 |
124 if (/^\$\d+$/.test(content)) | 126 if (/^\$\d+$/.test(content)) |
125 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; | 127 placeholders[parseInt(content.substr(1), 10) - 1] = placeholder; |
126 else | 128 else |
127 text = replacePlaceholder(text, placeholder, content); | 129 text = replacePlaceholder(text, placeholder, content); |
128 } | 130 } |
129 | 131 |
130 return [text, placeholders]; | 132 return [text, placeholders]; |
131 }; | 133 }; |
132 | 134 |
133 var readCatalog = function(locale, catalogFile) | 135 let readCatalog = function(locale, file) |
134 { | 136 { |
135 var xhr = new XMLHttpRequest(); | 137 let xhr = new XMLHttpRequest(); |
136 xhr.open("GET", "locale/" + locale + "/" + catalogFile, false); | 138 xhr.open("GET", "locale/" + locale + "/" + file, false); |
137 xhr.overrideMimeType("text/plain"); | 139 xhr.overrideMimeType("text/plain"); |
138 | 140 |
139 try | 141 try |
140 { | 142 { |
141 xhr.send(); | 143 xhr.send(); |
142 } | 144 } |
143 catch (e) | 145 catch (e) |
144 { | 146 { |
145 return; | 147 return; |
146 } | 148 } |
147 | 149 |
148 if (xhr.status != 200 && xhr.status != 0) | 150 if (xhr.status != 200 && xhr.status != 0) |
149 return; | 151 return; |
150 | 152 |
151 var rawCatalog = JSON.parse(xhr.responseText); | 153 let rawCatalog = JSON.parse(xhr.responseText); |
152 for (var msgId in rawCatalog) | 154 for (let msgId in rawCatalog) |
153 { | 155 { |
154 if (!(msgId in catalog)) | 156 if (!(msgId in catalog)) |
155 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 157 catalog[msgId] = parseMessage(rawCatalog[msgId]); |
156 } | 158 } |
157 }; | 159 }; |
158 | 160 |
159 global.ext.i18n = { | 161 window.ext.i18n = { |
160 locale: locales[0], | 162 locale: locales[0], |
161 getMessage: function(msgId, substitutions) | 163 getMessage(msgId, substitutions) |
162 { | 164 { |
163 while (true) | 165 while (true) |
164 { | 166 { |
165 var message = catalog[msgId]; | 167 let message = catalog[msgId]; |
166 if (message) | 168 if (message) |
167 { | 169 { |
168 var text = message[0]; | 170 let text = message[0]; |
169 var placeholders = message[1]; | 171 let placeholders = message[1]; |
170 | 172 |
171 if (!(substitutions instanceof Array)) | 173 if (!(substitutions instanceof Array)) |
172 substitutions = [substitutions]; | 174 substitutions = [substitutions]; |
173 | 175 |
174 for (var i = 0; i < placeholders.length; i++) | 176 for (let i = 0; i < placeholders.length; i++) |
175 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 177 text = replacePlaceholder(text, placeholders[i], substitutions[i]); |
176 | 178 |
177 return text; | 179 return text; |
178 } | 180 } |
179 | 181 |
180 if (locales.length == 0) | 182 if (locales.length == 0) |
181 return ""; | 183 return ""; |
182 | 184 |
183 var locale = locales.shift(); | 185 let locale = locales.shift(); |
184 readCatalog(locale, "common.json"); | 186 readCatalog(locale, "common.json"); |
185 readCatalog(locale, catalogFile); | 187 readCatalog(locale, catalogFile); |
186 } | 188 } |
187 } | 189 } |
188 }; | 190 }; |
189 })(this); | 191 }()); |
LEFT | RIGHT |