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

Unified Diff: ext/common.js

Issue 6427347985104896: Issue 1663 - Making first-run page testable in the adblockplusui repository (Closed)
Patch Set: More common approach to wrapping code Created Dec. 17, 2014, 1 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 | « no previous file | firstRun.html » ('j') | test_server.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/common.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/ext/common.js
@@ -0,0 +1,143 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 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/>.
+ */
+
+(function(global)
+{
+ /* I18n */
+
+ var getLocaleCandidates = function(selectedLocale)
+ {
+ var candidates = [];
+ var 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
+ var parts = selectedLocale.split("-");
+ var language = parts[0];
+ var region = (parts[1] || "").toUpperCase();
+
+ if (region)
+ candidates.push(language + "-" + region);
+
+ candidates.push(language);
+
+ if (candidates.indexOf(defaultLocale) == -1)
+ candidates.push(defaultLocale);
+
+ return candidates;
+ };
+
+ var initCatalog = function(uiLocale)
+ {
+ var bidiDir = /^(ar|fa|he|ug|ur)(-|$)/.test(uiLocale) ? "rtl" : "ltr";
+ var catalog = Object.create(null);
+
+ catalog["@@ui_locale"] = [uiLocale.replace(/-/g, "_"), []];
+ catalog["@@bidi_dir" ] = [bidiDir, []];
+
+ return catalog;
+ };
+
+ var selectedLocale = navigator.language;
Thomas Greiner 2014/12/17 13:43:40 Change to `global.navigator.language` for consiste
Wladimir Palant 2014/12/17 14:19:45 navigator isn't a property of generic global objec
Thomas Greiner 2014/12/17 15:13:52 Do you have a relevant scenario in mind in which w
Wladimir Palant 2014/12/17 15:25:54 Not here of course - but the implementation in Fir
+ var match = /[?&]locale=([\w\-]+)/.exec(window.location.search);
Thomas Greiner 2014/12/17 13:43:40 Change to `global.location.search`
+ if (match)
+ selectedLocale = match[1];
+
+ var locales = getLocaleCandidates(selectedLocale);
+ var catalog = initCatalog(locales[0]);
+ var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json";
Thomas Greiner 2014/12/17 13:43:40 Change to `global.location.pathname`
+
+ var replacePlaceholder = function(text, placeholder, content)
+ {
+ return text.split("$" + placeholder + "$").join(content || "");
+ };
+
+ var parseMessage = function(rawMessage)
+ {
+ var text = rawMessage.message;
+ var placeholders = [];
+
+ for (var placeholder in rawMessage.placeholders)
+ {
+ var content = rawMessage.placeholders[placeholder].content;
+
+ if (/^\$\d+$/.test(content))
+ placeholders[parseInt(content.substr(1), 10) - 1] = placeholder;
+ else
+ text = replacePlaceholder(text, placeholder, content);
+ }
+
+ return [text, placeholders];
+ };
+
+ var readCatalog = function(locale)
+ {
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", "locale/" + locale + "/" + catalogFile, false);
+ xhr.overrideMimeType("text/plain");
+
+ try
+ {
+ xhr.send();
+ }
+ catch (e)
+ {
+ return;
+ }
+
+ if (xhr.status != 200 && xhr.status != 0)
+ return;
+
+ var rawCatalog = JSON.parse(xhr.responseText);
+ for (var msgId in rawCatalog)
+ {
+ if (!(msgId in catalog))
+ catalog[msgId] = parseMessage(rawCatalog[msgId]);
+ }
+ };
+
+ if (!global.ext)
+ global.ext = {};
+
+ global.ext.i18n = {
+ getMessage: function(msgId, substitutions)
+ {
+ while (true)
+ {
+ var message = catalog[msgId];
+ if (message)
+ {
+ var text = message[0];
+ var placeholders = message[1];
+
+ if (!(substitutions instanceof Array))
+ substitutions = [substitutions];
+
+ for (var i = 0; i < placeholders.length; i++)
+ text = replacePlaceholder(text, placeholders[i], substitutions[i]);
+
+ return text;
+ }
+
+ if (locales.length == 0)
+ return "";
+
+ readCatalog(locales.shift());
+ }
+ }
+ };
+})(this);
« no previous file with comments | « no previous file | firstRun.html » ('j') | test_server.py » ('J')

Powered by Google App Engine
This is Rietveld