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 "use strict"; |
| 19 |
18 // This variable should no longer be necessary once options.js in Chrome | 20 // This variable should no longer be necessary once options.js in Chrome |
19 // accesses ext.i18n directly. | 21 // accesses ext.i18n directly. |
20 var i18n = ext.i18n; | 22 let {i18n} = ext; |
21 | 23 |
22 // Getting UI locale cannot be done synchronously on Firefox, | 24 // Getting UI locale cannot be done synchronously on Firefox, |
23 // requires messaging the background page. For Chrome and Safari, | 25 // requires messaging the background page. For Chrome and Safari, |
24 // we could get the UI locale here, but would need to duplicate | 26 // we could get the UI locale here, but would need to duplicate |
25 // the logic implemented in Utils.appLocale. | 27 // the logic implemented in Utils.appLocale. |
26 ext.backgroundPage.sendMessage( | 28 ext.backgroundPage.sendMessage( |
27 { | 29 { |
28 type: "app.get", | 30 type: "app.get", |
29 what: "localeInfo" | 31 what: "localeInfo" |
30 }, | 32 }, |
31 function(localeInfo) | 33 localeInfo => |
32 { | 34 { |
33 document.documentElement.lang = localeInfo.locale; | 35 document.documentElement.lang = localeInfo.locale; |
34 document.documentElement.dir = localeInfo.bidiDir; | 36 document.documentElement.dir = localeInfo.bidiDir; |
35 } | 37 } |
36 ); | 38 ); |
37 | 39 |
38 // Inserts i18n strings into matching elements. Any inner HTML already in the el
ement is | 40 // Inserts i18n strings into matching elements. Any inner HTML already |
39 // parsed as JSON and used as parameters to substitute into placeholders in the
i18n | 41 // in the element is parsed as JSON and used as parameters to |
40 // message. | 42 // substitute into placeholders in the i18n message. |
41 ext.i18n.setElementText = function(element, stringName, arguments) | 43 ext.i18n.setElementText = function(element, stringName, args) |
42 { | 44 { |
43 function processString(str, element) | 45 function processString(str, currentElement) |
44 { | 46 { |
45 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); | 47 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); |
46 if (match) | 48 if (match) |
47 { | 49 { |
48 processString(match[1], element); | 50 processString(match[1], currentElement); |
49 | 51 |
50 var e = document.createElement(match[2]); | 52 let e = document.createElement(match[2]); |
51 processString(match[3], e); | 53 processString(match[3], e); |
52 element.appendChild(e); | 54 currentElement.appendChild(e); |
53 | 55 |
54 processString(match[4], element); | 56 processString(match[4], currentElement); |
55 } | 57 } |
56 else | 58 else |
57 element.appendChild(document.createTextNode(str)); | 59 currentElement.appendChild(document.createTextNode(str)); |
58 } | 60 } |
59 | 61 |
60 while (element.lastChild) | 62 while (element.lastChild) |
61 element.removeChild(element.lastChild); | 63 element.removeChild(element.lastChild); |
62 processString(ext.i18n.getMessage(stringName, arguments), element); | 64 processString(ext.i18n.getMessage(stringName, args), element); |
63 } | 65 }; |
64 | 66 |
65 // Loads i18n strings | 67 // Loads i18n strings |
66 function loadI18nStrings() | 68 function loadI18nStrings() |
67 { | 69 { |
68 function addI18nStringsToElements(containerElement) | 70 function addI18nStringsToElements(containerElement) |
69 { | 71 { |
70 var elements = containerElement.querySelectorAll("[class^='i18n_']"); | 72 let elements = containerElement.querySelectorAll("[class^='i18n_']"); |
71 for(var i = 0; i < elements.length; i++) | 73 for (let i = 0; i < elements.length; i++) |
72 { | 74 { |
73 var node = elements[i]; | 75 let node = elements[i]; |
74 var arguments = JSON.parse("[" + node.textContent + "]"); | 76 let args = JSON.parse("[" + node.textContent + "]"); |
75 if (arguments.length == 0) | 77 if (args.length == 0) |
76 arguments = null; | 78 args = null; |
77 | 79 |
78 var className = node.className; | 80 let {className} = node; |
79 if (className instanceof SVGAnimatedString) | 81 if (className instanceof SVGAnimatedString) |
80 className = className.animVal; | 82 className = className.animVal; |
81 var stringName = className.split(/\s/)[0].substring(5); | 83 let stringName = className.split(/\s/)[0].substring(5); |
82 | 84 |
83 ext.i18n.setElementText(node, stringName, arguments); | 85 ext.i18n.setElementText(node, stringName, args); |
84 } | 86 } |
85 } | 87 } |
86 addI18nStringsToElements(document); | 88 addI18nStringsToElements(document); |
87 // Content of Template is not rendered on runtime so we need to add | 89 // Content of Template is not rendered on runtime so we need to add |
88 // translation strings for each Template documentFragment content individually
| 90 // translation strings for each Template documentFragment content |
89 var templates = document.querySelectorAll("template"); | 91 // individually. |
90 for (var i = 0; i < templates.length; i++) | 92 let templates = document.querySelectorAll("template"); |
| 93 for (let i = 0; i < templates.length; i++) |
91 addI18nStringsToElements(templates[i].content); | 94 addI18nStringsToElements(templates[i].content); |
92 } | 95 } |
93 | 96 |
| 97 /* eslint-disable camelcase */ |
94 // Provides a more readable string of the current date and time | 98 // Provides a more readable string of the current date and time |
95 function i18n_timeDateStrings(when) | 99 function i18n_timeDateStrings(when) |
96 { | 100 { |
97 var d = new Date(when); | 101 let d = new Date(when); |
98 var timeString = d.toLocaleTimeString(); | 102 let timeString = d.toLocaleTimeString(); |
99 | 103 |
100 var now = new Date(); | 104 let now = new Date(); |
101 if (d.toDateString() == now.toDateString()) | 105 if (d.toDateString() == now.toDateString()) |
102 return [timeString]; | 106 return [timeString]; |
103 else | 107 return [timeString, d.toLocaleDateString()]; |
104 return [timeString, d.toLocaleDateString()]; | |
105 } | 108 } |
106 | 109 |
107 // Formats date string to ["YYYY-MM-DD", "mm:ss"] format | 110 // Formats date string to ["YYYY-MM-DD", "mm:ss"] format |
108 function i18n_formatDateTime(when) | 111 function i18n_formatDateTime(when) |
109 { | 112 { |
110 var date = new Date(when); | 113 let date = new Date(when); |
111 var dateParts = [date.getFullYear(), date.getMonth() + 1, date.getDate(), | 114 let dateParts = [date.getFullYear(), date.getMonth() + 1, date.getDate(), |
112 date.getHours(), date.getMinutes()]; | 115 date.getHours(), date.getMinutes()]; |
113 | 116 |
114 var dateParts = dateParts.map(function(datePart) | 117 dateParts = dateParts.map( |
115 { | 118 datePart => datePart < 10 ? "0" + datePart : datePart |
116 return datePart < 10 ? "0" + datePart : datePart; | 119 ); |
117 }); | |
118 | 120 |
119 return [dateParts.splice(0, 3).join("-"), dateParts.join(":")]; | 121 return [dateParts.splice(0, 3).join("-"), dateParts.join(":")]; |
120 } | 122 } |
| 123 /* eslint-enable camelcase */ |
121 | 124 |
122 // Fill in the strings as soon as possible | 125 // Fill in the strings as soon as possible |
123 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); | 126 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); |
OLD | NEW |