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

Unified Diff: polyfill.js

Issue 29573083: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockplusui/
Patch Set: Add polyfill.js to README.md Created Oct. 17, 2017, 12:35 p.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
« no previous file with comments | « mobile-options.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: polyfill.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/polyfill.js
@@ -0,0 +1,140 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+"use strict";
+
+(function()
+{
+ window.browser = {};
+
+ /* I18n */
+
+ let getLocaleCandidates = function(selectedLocale)
+ {
+ let candidates = [];
+ let defaultLocale = "en_US";
+
+ // e.g. "ja-jp-mac" -> "ja_JP", note that the part after the second
+ // dash is dropped, since we only support language and region
+ let parts = selectedLocale.split("-");
+ let language = parts[0];
+ let region = (parts[1] || "").toUpperCase();
+
+ if (region)
+ candidates.push(language + "_" + region);
+
+ candidates.push(language);
+
+ if (candidates.indexOf(defaultLocale) == -1)
+ candidates.push(defaultLocale);
+
+ return candidates;
+ };
+
+ let selectedLocale = window.navigator.language;
+ let match = /[?&]locale=([\w-]+)/.exec(window.location.search);
+ if (match)
+ selectedLocale = match[1];
+
+ let locales = getLocaleCandidates(selectedLocale);
+ let catalog = Object.create(null);
+ let catalogFile = window.location.pathname.replace(/.*\//, "")
+ .replace(/\..*/, "") + ".json";
+
+ let replacePlaceholder = function(text, placeholder, content)
+ {
+ return text.split("$" + placeholder + "$").join(content || "");
+ };
+
+ let parseMessage = function(rawMessage)
+ {
+ let text = rawMessage.message;
+ let placeholders = [];
+
+ for (let placeholder in rawMessage.placeholders)
+ {
+ let {content} = rawMessage.placeholders[placeholder];
+
+ if (/^\$\d+$/.test(content))
+ placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
+ else
+ text = replacePlaceholder(text, placeholder, content);
+ }
+
+ return [text, placeholders];
+ };
+
+ let readCatalog = function(locale, file)
+ {
+ let xhr = new XMLHttpRequest();
+ xhr.open("GET", "locale/" + locale + "/" + file, false);
+ xhr.overrideMimeType("text/plain");
+
+ try
+ {
+ xhr.send();
+ }
+ catch (e)
+ {
+ return;
+ }
+
+ if (xhr.status != 200 && xhr.status != 0)
+ return;
+
+ let rawCatalog = JSON.parse(xhr.responseText);
+ for (let msgId in rawCatalog)
+ {
+ if (!(msgId in catalog))
+ catalog[msgId] = parseMessage(rawCatalog[msgId]);
+ }
+ };
+
+ browser.i18n = {
+ getUILanguage()
+ {
+ return locales[0].replace(/_/g, "-");
+ },
+ getMessage(msgId, substitutions)
+ {
+ while (true)
+ {
+ let message = catalog[msgId];
+ if (message)
+ {
+ let text = message[0];
+ let placeholders = message[1];
+
+ if (!(substitutions instanceof Array))
+ substitutions = [substitutions];
+
+ for (let i = 0; i < placeholders.length; i++)
+ text = replacePlaceholder(text, placeholders[i], substitutions[i]);
+
+ return text;
+ }
+
+ if (locales.length == 0)
+ return "";
+
+ let locale = locales.shift();
+ readCatalog(locale, "common.json");
+ readCatalog(locale, catalogFile);
+ }
+ }
+ };
+}());
« no previous file with comments | « mobile-options.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld