[Pkg-mozext-commits] [adblock-plus] 15/52: Issue 1663 - Added ext.i18n implementation for testing
David Prévot
taffit at moszumanska.debian.org
Thu Jan 22 21:43:44 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository adblock-plus.
commit 82e3151135753953b5492e07e87982ed73c68886
Author: Wladimir Palant <trev at adblockplus.org>
Date: Wed Dec 17 15:18:07 2014 +0100
Issue 1663 - Added ext.i18n implementation for testing
--HG--
extra : histedit_source : a09f39d22dd0cd3115f016b5d174333cd5c1f844
---
ext/common.js | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 143 insertions(+)
diff --git a/ext/common.js b/ext/common.js
new file mode 100644
index 0000000..434b476
--- /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 = window.navigator.language;
+ var match = /[?&]locale=([\w\-]+)/.exec(window.location.search);
+ if (match)
+ selectedLocale = match[1];
+
+ var locales = getLocaleCandidates(selectedLocale);
+ var catalog = initCatalog(locales[0]);
+ var catalogFile = window.location.pathname.replace(/.*\//, "").replace(/\..*/, "") + ".json";
+
+ 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);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git
More information about the Pkg-mozext-commits
mailing list