Index: i18n.js |
diff --git a/i18n.js b/i18n.js |
index 6db0881fd0138d9301cd0f3a6214282f965a216c..b00e5b18f9ff4b67cfdbd1bde8b905ec1e443fd8 100644 |
--- a/i18n.js |
+++ b/i18n.js |
@@ -15,9 +15,11 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+"use strict"; |
+ |
// This variable should no longer be necessary once options.js in Chrome |
// accesses ext.i18n directly. |
-var i18n = ext.i18n; |
+let {i18n} = ext; |
// Getting UI locale cannot be done synchronously on Firefox, |
// requires messaging the background page. For Chrome and Safari, |
@@ -28,93 +30,92 @@ ext.backgroundPage.sendMessage( |
type: "app.get", |
what: "localeInfo" |
}, |
- function(localeInfo) |
+ localeInfo => |
{ |
document.documentElement.lang = localeInfo.locale; |
document.documentElement.dir = localeInfo.bidiDir; |
} |
); |
-// Inserts i18n strings into matching elements. Any inner HTML already in the element is |
-// parsed as JSON and used as parameters to substitute into placeholders in the i18n |
-// message. |
-ext.i18n.setElementText = function(element, stringName, arguments) |
+// Inserts i18n strings into matching elements. Any inner HTML already |
+// in the element is parsed as JSON and used as parameters to |
+// substitute into placeholders in the i18n message. |
+ext.i18n.setElementText = function(element, stringName, args) |
{ |
- function processString(str, element) |
+ function processString(str, currentElement) |
{ |
- var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); |
+ let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); |
if (match) |
{ |
- processString(match[1], element); |
+ processString(match[1], currentElement); |
- var e = document.createElement(match[2]); |
+ let e = document.createElement(match[2]); |
processString(match[3], e); |
- element.appendChild(e); |
+ currentElement.appendChild(e); |
- processString(match[4], element); |
+ processString(match[4], currentElement); |
} |
else |
- element.appendChild(document.createTextNode(str)); |
+ currentElement.appendChild(document.createTextNode(str)); |
} |
while (element.lastChild) |
element.removeChild(element.lastChild); |
- processString(ext.i18n.getMessage(stringName, arguments), element); |
-} |
+ processString(ext.i18n.getMessage(stringName, args), element); |
+}; |
// Loads i18n strings |
function loadI18nStrings() |
{ |
function addI18nStringsToElements(containerElement) |
{ |
- var elements = containerElement.querySelectorAll("[class^='i18n_']"); |
- for(var i = 0; i < elements.length; i++) |
+ let elements = containerElement.querySelectorAll("[class^='i18n_']"); |
+ for (let i = 0; i < elements.length; i++) |
{ |
- var node = elements[i]; |
- var arguments = JSON.parse("[" + node.textContent + "]"); |
- if (arguments.length == 0) |
- arguments = null; |
+ let node = elements[i]; |
+ let args = JSON.parse("[" + node.textContent + "]"); |
+ if (args.length == 0) |
+ args = null; |
- var className = node.className; |
+ let {className} = node; |
if (className instanceof SVGAnimatedString) |
className = className.animVal; |
- var stringName = className.split(/\s/)[0].substring(5); |
+ let stringName = className.split(/\s/)[0].substring(5); |
- ext.i18n.setElementText(node, stringName, arguments); |
+ ext.i18n.setElementText(node, stringName, args); |
} |
} |
addI18nStringsToElements(document); |
// Content of Template is not rendered on runtime so we need to add |
- // translation strings for each Template documentFragment content individually |
- var templates = document.querySelectorAll("template"); |
- for (var i = 0; i < templates.length; i++) |
+ // translation strings for each Template documentFragment content |
+ // individually. |
+ let templates = document.querySelectorAll("template"); |
+ for (let i = 0; i < templates.length; i++) |
addI18nStringsToElements(templates[i].content); |
} |
// Provides a more readable string of the current date and time |
-function i18n_timeDateStrings(when) |
+function i18nTimeDateStrings(when) |
{ |
- var d = new Date(when); |
- var timeString = d.toLocaleTimeString(); |
+ let d = new Date(when); |
+ let timeString = d.toLocaleTimeString(); |
- var now = new Date(); |
+ let now = new Date(); |
if (d.toDateString() == now.toDateString()) |
return [timeString]; |
- else |
- return [timeString, d.toLocaleDateString()]; |
+ return [timeString, d.toLocaleDateString()]; |
} |
// Formats date string to ["YYYY-MM-DD", "mm:ss"] format |
-function i18n_formatDateTime(when) |
+function i18nFormatDateTime(when) |
{ |
- var date = new Date(when); |
- var dateParts = [date.getFullYear(), date.getMonth() + 1, date.getDate(), |
- date.getHours(), date.getMinutes()]; |
+ let date = new Date(when); |
+ let dateParts = [date.getFullYear(), date.getMonth() + 1, date.getDate(), |
+ date.getHours(), date.getMinutes()]; |
- var dateParts = dateParts.map(function(datePart) |
- { |
- return datePart < 10 ? "0" + datePart : datePart; |
- }); |
+ dateParts = dateParts.map( |
+ datePart => datePart < 10 ? "0" + datePart : datePart |
+ ); |
return [dateParts.splice(0, 3).join("-"), dateParts.join(":")]; |
} |