OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 (function(global) |
19 { | 19 { |
| 20 if (!global.ext) |
| 21 global.ext = {}; |
| 22 |
| 23 function Page(source) |
| 24 { |
| 25 this._source = source; |
| 26 } |
| 27 Page.prototype = |
| 28 { |
| 29 sendMessage: function(message) |
| 30 { |
| 31 this._source.postMessage({ |
| 32 type: "message", |
| 33 messageId: -1, |
| 34 payload: message |
| 35 }, "*"); |
| 36 } |
| 37 } |
| 38 |
| 39 /* Message passing */ |
| 40 |
| 41 global.ext.onMessage = |
| 42 { |
| 43 addListener: function(listener) |
| 44 { |
| 45 listener._extWrapper = function(event) |
| 46 { |
| 47 if (event.data.type != "message") |
| 48 return; |
| 49 |
| 50 var message = event.data.payload; |
| 51 var messageId = event.data.messageId; |
| 52 var sender = { |
| 53 page: new Page(event.source) |
| 54 }; |
| 55 var callback = function(message) |
| 56 { |
| 57 event.source.postMessage({ |
| 58 type: "response", |
| 59 messageId: messageId, |
| 60 payload: message |
| 61 }, "*"); |
| 62 }; |
| 63 listener(message, sender, callback); |
| 64 }; |
| 65 window.addEventListener("message", listener._extWrapper, false); |
| 66 }, |
| 67 |
| 68 removeListener: function(listener) |
| 69 { |
| 70 if ("_extWrapper" in listener) |
| 71 window.removeEventListener("message", listener._extWrapper, false); |
| 72 } |
| 73 }; |
| 74 |
20 /* I18n */ | 75 /* I18n */ |
21 | 76 |
22 var getLocaleCandidates = function(selectedLocale) | 77 var getLocaleCandidates = function(selectedLocale) |
23 { | 78 { |
24 var candidates = []; | 79 var candidates = []; |
25 var defaultLocale = "en-US"; | 80 var defaultLocale = "en-US"; |
26 | 81 |
27 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second | 82 // e.g. "ja-jp-mac" -> "ja-JP", note that the part after the second |
28 // dash is dropped, since we only support language and region | 83 // dash is dropped, since we only support language and region |
29 var parts = selectedLocale.split("-"); | 84 var parts = selectedLocale.split("-"); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 return; | 158 return; |
104 | 159 |
105 var rawCatalog = JSON.parse(xhr.responseText); | 160 var rawCatalog = JSON.parse(xhr.responseText); |
106 for (var msgId in rawCatalog) | 161 for (var msgId in rawCatalog) |
107 { | 162 { |
108 if (!(msgId in catalog)) | 163 if (!(msgId in catalog)) |
109 catalog[msgId] = parseMessage(rawCatalog[msgId]); | 164 catalog[msgId] = parseMessage(rawCatalog[msgId]); |
110 } | 165 } |
111 }; | 166 }; |
112 | 167 |
113 if (!global.ext) | |
114 global.ext = {}; | |
115 | |
116 global.ext.i18n = { | 168 global.ext.i18n = { |
117 getMessage: function(msgId, substitutions) | 169 getMessage: function(msgId, substitutions) |
118 { | 170 { |
119 while (true) | 171 while (true) |
120 { | 172 { |
121 var message = catalog[msgId]; | 173 var message = catalog[msgId]; |
122 if (message) | 174 if (message) |
123 { | 175 { |
124 var text = message[0]; | 176 var text = message[0]; |
125 var placeholders = message[1]; | 177 var placeholders = message[1]; |
126 | 178 |
127 if (!(substitutions instanceof Array)) | 179 if (!(substitutions instanceof Array)) |
128 substitutions = [substitutions]; | 180 substitutions = [substitutions]; |
129 | 181 |
130 for (var i = 0; i < placeholders.length; i++) | 182 for (var i = 0; i < placeholders.length; i++) |
131 text = replacePlaceholder(text, placeholders[i], substitutions[i]); | 183 text = replacePlaceholder(text, placeholders[i], substitutions[i]); |
132 | 184 |
133 return text; | 185 return text; |
134 } | 186 } |
135 | 187 |
136 if (locales.length == 0) | 188 if (locales.length == 0) |
137 return ""; | 189 return ""; |
138 | 190 |
139 readCatalog(locales.shift()); | 191 readCatalog(locales.shift()); |
140 } | 192 } |
141 } | 193 } |
142 }; | 194 }; |
143 })(this); | 195 })(this); |
OLD | NEW |