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

Side by Side Diff: chrome/content/ui/i18n.js

Issue 10585038: First-run page (revisited) (Closed)
Patch Set: Implemented behavior of remaining buttons on Chrome; Added changelog and data corruption warning Created May 24, 2013, 10:09 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 var i18n;
19 if (typeof chrome != "undefined")
20 {
21 i18n = chrome.i18n;
22 }
23 else
24 {
Wladimir Palant 2013/05/27 11:02:00 Please add a comment noting that this is the Firef
Thomas Greiner 2013/05/27 13:03:16 Done.
25 // Randomize URI to work around bug 719376
26 var stringBundle = Services.strings.createBundle("chrome://" + require("info") .addonName + "/locale/firstRun.properties?" + Math.random());
Wladimir Palant 2013/05/27 11:02:00 A relative URL should work here: "/locale/firstRun
Thomas Greiner 2013/05/27 13:03:16 Done.
27 function getI18nMessage(key)
28 {
29 return {
30 "message": stringBundle.GetStringFromName(key)
31 };
32 }
33
34 i18n = (function()
35 {
36 function getText(message, args)
37 {
38 var text = message.message;
39 var placeholders = message.placeholders;
40
41 if (!args || !placeholders)
42 return text;
43
44 for (var key in placeholders)
45 {
46 var content = placeholders[key].content;
47 if (!content)
48 continue;
49
50 var index = parseInt(content.slice(1), 10);
51 if (isNaN(index))
52 continue;
53
54 var replacement = args[index - 1];
55 if (typeof replacement === "undefined")
56 continue;
57
58 text = text.split("$" + key + "$").join(replacement);
59 }
60 return text;
61 }
62
63 return {
64 getMessage: function(key, args)
65 {
66 var message = getI18nMessage(key);
67 if (!message)
68 return "Missing translation: " + key;
Wladimir Palant 2013/05/27 11:02:00 This won't work - GetStringForName throws on missi
Thomas Greiner 2013/05/27 13:03:16 Done.
69 return getText(message, args);
70 }
71 };
72 })();
73 }
74
75 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the
76 // element is parsed as JSON and used as parameters to substitute into placehold ers in the
77 // i18n message.
78 function loadI18nStrings()
79 {
80 var nodes = document.querySelectorAll("[class^='i18n_']");
81 for(var i = 0; i < nodes.length; i++)
82 {
83 var arguments = JSON.parse("[" + nodes[i].textContent + "]");
84 var className = nodes[i].className;
85 if (className instanceof SVGAnimatedString)
86 className = className.animVal;
87 var stringName = className.split(/\s/)[0].substring(5);
88 var prop = "innerHTML" in nodes[i] ? "innerHTML" : "textContent";
89 if(arguments.length > 0)
90 nodes[i][prop] = i18n.getMessage(stringName, arguments);
91 else
92 nodes[i][prop] = i18n.getMessage(stringName);
93 }
94 }
95
96 // Provides a more readable string of the current date and time
97 function i18n_timeDateStrings(when)
98 {
99 var d = new Date(when);
100 var timeString = d.toLocaleTimeString();
101
102 var now = new Date();
103 if (d.toDateString() == now.toDateString())
104 return [timeString];
105 else
106 return [timeString, d.toLocaleDateString()];
107 }
108
109 // Fill in the strings as soon as possible
110 // Script could be injected after DOMContentLoaded fired
Wladimir Palant 2013/05/27 11:02:00 Nope, it could not - if firstRun.js is coded clean
Thomas Greiner 2013/05/27 13:03:16 Done.
111 if (document.readyState != "loading")
112 loadI18nStrings();
113 else
114 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW

Powered by Google App Engine
This is Rietveld