[Pkg-mozext-commits] [adblock-plus] 68/98: Issue 4915 - Expose ext.i18n for background pages
David Prévot
taffit at moszumanska.debian.org
Tue Oct 24 01:30:21 UTC 2017
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository adblock-plus.
commit b25ca525ae5f3fbf675c754067d3d2200d1fa23b
Author: Winsley von Spee <winsley at adblockplus.org>
Date: Wed Mar 29 14:21:23 2017 +0200
Issue 4915 - Expose ext.i18n for background pages
---
ext/background.js | 3 ++-
ext/common.js | 28 +++++++++++++++++++++++++
ext/content.js | 61 -------------------------------------------------------
3 files changed, 30 insertions(+), 62 deletions(-)
diff --git a/ext/background.js b/ext/background.js
index ec78f0a..b57f96c 100644
--- a/ext/background.js
+++ b/ext/background.js
@@ -18,10 +18,11 @@
let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", null);
let {Services} = Cu.import("resource://gre/modules/Services.jsm", null);
-let {_EventTarget: EventTarget} = require("ext_common");
+let {_EventTarget: EventTarget, i18n} = require("ext_common");
let {port} = require("messaging");
exports.onMessage = new EventTarget(port);
+exports.i18n = i18n;
function Page(windowID)
{
diff --git a/ext/common.js b/ext/common.js
index 82e240b..1f0219a 100644
--- a/ext/common.js
+++ b/ext/common.js
@@ -17,6 +17,10 @@
(function(global)
{
+ const Cu = Components.utils;
+
+ let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
if (!global.ext)
global.ext = {};
@@ -82,6 +86,30 @@
}
};
+ let pageName = "global";
+ if (typeof location !== "undefined")
+ pageName = location.pathname.replace(/.*\//, "").replace(/\..*?$/, "");
+
+ let stringBundle = Services.strings.createBundle(
+ "chrome://adblockplus/locale/" + pageName + ".properties?" + Math.random());
+
+ global.ext.i18n = {
+ getMessage(key, args)
+ {
+ try {
+ return stringBundle.GetStringFromName(key);
+ }
+ catch(e)
+ {
+ // Don't report errors for special strings, these are expected to be
+ // missing.
+ if (key[0] != "@")
+ Cu.reportError(e);
+ return "";
+ }
+ }
+ };
+
if (typeof exports == "object")
exports = global.ext;
})(this);
diff --git a/ext/content.js b/ext/content.js
index ac20a37..366325a 100644
--- a/ext/content.js
+++ b/ext/content.js
@@ -79,65 +79,4 @@
}
};
- /* i18n */
- global.ext.i18n = (function()
- {
- var pageName = location.pathname.replace(/.*\//, "").replace(/\..*?$/, "");
-
- // Randomize URI to work around bug 719376
- var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/" + pageName +
- ".properties?" + Math.random());
-
- function getI18nMessage(key)
- {
- return {
- "message": stringBundle.GetStringFromName(key)
- };
- }
-
- function getText(message, args)
- {
- var text = message.message;
- var placeholders = message.placeholders;
-
- if (!args || !placeholders)
- return text;
-
- for (var key in placeholders)
- {
- var content = placeholders[key].content;
- if (!content)
- continue;
-
- var index = parseInt(content.slice(1), 10);
- if (isNaN(index))
- continue;
-
- var replacement = args[index - 1];
- if (typeof replacement === "undefined")
- continue;
-
- text = text.split("$" + key + "$").join(replacement);
- }
- return text;
- }
-
- return {
- getMessage: function(key, args)
- {
- try{
- var message = getI18nMessage(key);
- return getText(message, args);
- }
- catch(e)
- {
- // Don't report errors for special strings, these are expected to be
- // missing.
- if (key[0] != "@")
- Cu.reportError(e);
- return "";
- }
- }
- };
- })();
})(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