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