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

Delta Between Two Patch Sets: ext/common.js

Issue 29376555: [adblockplus] Issue 4915 - Expose ext.i18n for background pages (Closed)
Left Patch Set: Created Feb. 20, 2017, 1:40 p.m.
Right Patch Set: Removed I18n class Created March 2, 2017, 5:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ext/background.js ('k') | ext/content.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function(global) 18 (function(global)
19 { 19 {
20 const Cu = Components.utils; 20 const Cu = Components.utils;
21 21
22 var Services = Cu.import("resource://gre/modules/Services.jsm", {}).Services; 22 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
Sebastian Noack 2017/02/20 16:53:20 Please use let in favor of var in new code. (Same
wspee 2017/03/01 14:54:08 Done.
23 23
24 if (!global.ext) 24 if (!global.ext)
25 global.ext = {}; 25 global.ext = {};
26 26
27 var wrapperSymbol = Symbol("ext-wrapper"); 27 var wrapperSymbol = Symbol("ext-wrapper");
28 28
29 function wrapFrames(frames) 29 function wrapFrames(frames)
30 { 30 {
31 if (!frames.length) 31 if (!frames.length)
32 return null; 32 return null;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 this._port.on("ext_message", wrapper); 79 this._port.on("ext_message", wrapper);
80 }, 80 },
81 81
82 removeListener: function(listener) 82 removeListener: function(listener)
83 { 83 {
84 if (listener[wrapperSymbol]) 84 if (listener[wrapperSymbol])
85 this._port.off("ext_message", listener[wrapperSymbol]); 85 this._port.off("ext_message", listener[wrapperSymbol]);
86 } 86 }
87 }; 87 };
88 88
89 let pageName = "global";
90 if (typeof location !== "undefined")
91 pageName = location.pathname.replace(/.*\//, "").replace(/\..*?$/, "");
89 92
90 global.ext.i18n = (function() 93 let stringBundle = Services.strings.createBundle(
Sebastian Noack 2017/02/20 16:53:20 There is no need for any IIFE in modern code. The
Sebastian Noack 2017/02/20 16:53:20 It seems you forgot to remove ext.i18n from ext/co
wspee 2017/03/01 14:54:08 Done.
wspee 2017/03/01 14:54:08 Done.
91 { 94 "chrome://adblockplus/locale/" + pageName + ".properties?" + Math.random());
92 if (typeof location == "undefined")
wspee 2017/02/20 13:53:16 This is probably a bad idea, but I'm not sure what
Sebastian Noack 2017/02/20 16:53:20 I might misunderstand, but isn't the "global" bund
wspee 2017/03/01 14:54:08 Yes it is, it does the right thing. I was thinkin
93 var pageName = "global";
94 else
95 var pageName = location.pathname.replace(/.*\//, "").replace(/\..*?$/, "") ;
96 95
97 // Randomize URI to work around bug 719376 96 global.ext.i18n = {
98 var stringBundle = Services.strings.createBundle("chrome://adblockplus/local e/" + pageName + 97 getMessage(key, args)
99 ".properties?" + Math.random());
100
101 function getI18nMessage(key)
102 { 98 {
103 return { 99 try {
104 "message": stringBundle.GetStringFromName(key) 100 return stringBundle.GetStringFromName(key);
105 }; 101 }
102 catch(e)
103 {
104 // Don't report errors for special strings, these are expected to be
105 // missing.
106 if (key[0] != "@")
107 Cu.reportError(e);
108 return "";
109 }
106 } 110 }
107 111 };
108 function getText(message, args)
109 {
110 var text = message.message;
Sebastian Noack 2017/02/20 16:53:20 Perhaps you rather want to use restructuring argum
wspee 2017/03/01 14:54:08 I have removed getText and getI18nMessage altogeth
111 var placeholders = message.placeholders;
112
113 if (!args || !placeholders)
114 return text;
115
116 for (var key in placeholders)
117 {
118 var content = placeholders[key].content;
119 if (!content)
120 continue;
121
122 var index = parseInt(content.slice(1), 10);
123 if (isNaN(index))
124 continue;
125
126 var replacement = args[index - 1];
127 if (typeof replacement === "undefined")
128 continue;
129
130 text = text.split("$" + key + "$").join(replacement);
131 }
132 return text;
133 }
134
135 return {
136 getMessage: function(key, args)
137 {
138 try{
139 var message = getI18nMessage(key);
140 return getText(message, args);
141 }
142 catch(e)
143 {
144 // Don't report errors for special strings, these are expected to be
145 // missing.
146 if (key[0] != "@")
147 Cu.reportError(e);
148 return "";
149 }
150 }
151 };
152 })();
153 112
154 if (typeof exports == "object") 113 if (typeof exports == "object")
155 exports = global.ext; 114 exports = global.ext;
156 })(this); 115 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld