Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: i18n.js

Issue 29375899: Issue 4871 - Start using ESLint for adblockplusui (Closed)
Patch Set: Fix regressions with the new options page Created March 1, 2017, 8:25 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: i18n.js
diff --git a/i18n.js b/i18n.js
index 6db0881fd0138d9301cd0f3a6214282f965a216c..2aa30fcca81a8475cc33c78399eebabf38a77898 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,89 @@ 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++)
+ for (let node of containerElement.querySelectorAll("[class^='i18n_']"))
Thomas Greiner 2017/03/01 17:39:35 Detail: Again, it'd be nice if we could keep this
kzar 2017/03/07 12:48:32 Done.
{
- var node = elements[i];
- var arguments = JSON.parse("[" + node.textContent + "]");
- if (arguments.length == 0)
- arguments = null;
+ 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++)
- addI18nStringsToElements(templates[i].content);
+ // translation strings for each Template documentFragment content
+ // individually.
+ for (let template of document.querySelectorAll("template"))
+ addI18nStringsToElements(template.content);
}
// Provides a more readable string of the current date and time
-function i18n_timeDateStrings(when)
+function i18nTimeDateStrings(when)
Thomas Greiner 2017/03/01 17:39:35 Note that this function is used in the current opt
kzar 2017/03/02 04:36:01 Yep, don't worry I've done that. Actually come to
Thomas Greiner 2017/03/07 13:33:01 Great, thanks.
{
- 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(":")];
}

Powered by Google App Engine
This is Rietveld