| 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 // This variable should no longer be necessary once options.js in Chrome | |
| 21 // accesses ext.i18n directly. | |
| 22 let {i18n} = ext; | |
| 23 | |
| 24 // Getting UI locale cannot be done synchronously on Firefox, | 20 // Getting UI locale cannot be done synchronously on Firefox, |
| 25 // requires messaging the background page. For Chrome and Safari, | 21 // requires messaging the background page. For Chrome and Safari, |
| 26 // we could get the UI locale here, but would need to duplicate | 22 // we could get the UI locale here, but would need to duplicate |
| 27 // the logic implemented in Utils.appLocale. | 23 // the logic implemented in Utils.appLocale. |
| 28 ext.backgroundPage.sendMessage( | 24 ext.backgroundPage.sendMessage( |
| 29 { | 25 { |
| 30 type: "app.get", | 26 type: "app.get", |
| 31 what: "localeInfo" | 27 what: "localeInfo" |
| 32 }, | 28 }, |
| 33 (localeInfo) => | 29 (localeInfo) => |
| 34 { | 30 { |
| 35 document.documentElement.lang = localeInfo.locale; | 31 document.documentElement.lang = localeInfo.locale; |
| 36 document.documentElement.dir = localeInfo.bidiDir; | 32 document.documentElement.dir = localeInfo.bidiDir; |
| 37 } | 33 } |
| 38 ); | 34 ); |
| 39 | 35 |
| 40 // Inserts i18n strings into matching elements. Any inner HTML already | 36 ext.i18n = { |
| 41 // in the element is parsed as JSON and used as parameters to | 37 // Inserts i18n strings into matching elements. Any inner HTML already |
| 42 // substitute into placeholders in the i18n message. | 38 // in the element is parsed as JSON and used as parameters to |
| 43 ext.i18n.setElementText = function(element, stringName, args) | 39 // substitute into placeholders in the i18n message. |
| 44 { | 40 setElementText(element, stringName, args) |
| 45 function processString(str, currentElement) | |
| 46 { | 41 { |
| 47 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); | 42 function processString(str, currentElement) |
| 48 if (match) | |
| 49 { | 43 { |
| 50 processString(match[1], currentElement); | 44 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); |
| 45 if (match) |
| 46 { |
| 47 processString(match[1], currentElement); |
| 51 | 48 |
| 52 let e = document.createElement(match[2]); | 49 let e = document.createElement(match[2]); |
| 53 processString(match[3], e); | 50 processString(match[3], e); |
| 54 currentElement.appendChild(e); | 51 currentElement.appendChild(e); |
| 55 | 52 |
| 56 processString(match[4], currentElement); | 53 processString(match[4], currentElement); |
| 54 } |
| 55 else |
| 56 currentElement.appendChild(document.createTextNode(str)); |
| 57 } | 57 } |
| 58 else | 58 |
| 59 currentElement.appendChild(document.createTextNode(str)); | 59 while (element.lastChild) |
| 60 element.removeChild(element.lastChild); |
| 61 processString(chrome.i18n.getMessage(stringName, args), element); |
| 60 } | 62 } |
| 61 | |
| 62 while (element.lastChild) | |
| 63 element.removeChild(element.lastChild); | |
| 64 processString(ext.i18n.getMessage(stringName, args), element); | |
| 65 }; | 63 }; |
| 66 | 64 |
| 67 // Loads i18n strings | 65 // Loads i18n strings |
| 68 function loadI18nStrings() | 66 function loadI18nStrings() |
| 69 { | 67 { |
| 70 function addI18nStringsToElements(containerElement) | 68 function addI18nStringsToElements(containerElement) |
| 71 { | 69 { |
| 72 let elements = containerElement.querySelectorAll("[class^='i18n_']"); | 70 let elements = containerElement.querySelectorAll("[class^='i18n_']"); |
| 73 for (let node of elements) | 71 for (let node of elements) |
| 74 { | 72 { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 | 111 |
| 114 dateParts = dateParts.map( | 112 dateParts = dateParts.map( |
| 115 (datePart) => datePart < 10 ? "0" + datePart : datePart | 113 (datePart) => datePart < 10 ? "0" + datePart : datePart |
| 116 ); | 114 ); |
| 117 | 115 |
| 118 return [dateParts.splice(0, 3).join("-"), dateParts.join(":")]; | 116 return [dateParts.splice(0, 3).join("-"), dateParts.join(":")]; |
| 119 } | 117 } |
| 120 | 118 |
| 121 // Fill in the strings as soon as possible | 119 // Fill in the strings as soon as possible |
| 122 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); | 120 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); |
| OLD | NEW |