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

Delta Between Two Patch Sets: chrome/content/ui/i18n.js

Issue 10585038: First-run page (revisited) (Closed)
Left Patch Set: Created May 14, 2013, 3:12 p.m.
Right Patch Set: Created May 28, 2013, 2:09 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/ui/firstRun.js ('k') | chrome/content/ui/utils.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
(no file at all)
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 {
25 // Using Firefox' approach on i18n instead
26
27 // Randomize URI to work around bug 719376
28 var pageName = location.pathname.replace(/.*\//, '').replace(/\..*?$/, '');
29 var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/ " + pageName +
30 ".properties?" + Math.random());
31
32 function getI18nMessage(key)
33 {
34 return {
35 "message": stringBundle.GetStringFromName(key)
36 };
37 }
38
39 i18n = (function()
40 {
41 function getText(message, args)
42 {
43 var text = message.message;
44 var placeholders = message.placeholders;
45
46 if (!args || !placeholders)
47 return text;
48
49 for (var key in placeholders)
50 {
51 var content = placeholders[key].content;
52 if (!content)
53 continue;
54
55 var index = parseInt(content.slice(1), 10);
56 if (isNaN(index))
57 continue;
58
59 var replacement = args[index - 1];
60 if (typeof replacement === "undefined")
61 continue;
62
63 text = text.split("$" + key + "$").join(replacement);
64 }
65 return text;
66 }
67
68 return {
69 getMessage: function(key, args)
70 {
71 try{
72 var message = getI18nMessage(key);
73 return getText(message, args);
74 }
75 catch(e)
76 {
77 Cu.reportError(e);
78 return "Missing translation: " + key;
79 }
80 }
81 };
82 })();
83 }
84
85 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the
86 // element is parsed as JSON and used as parameters to substitute into placehold ers in the
87 // i18n message.
88 function loadI18nStrings()
89 {
90 var nodes = document.querySelectorAll("[class^='i18n_']");
91 for(var i = 0; i < nodes.length; i++)
92 {
93 var arguments = JSON.parse("[" + nodes[i].textContent + "]");
94 var className = nodes[i].className;
95 if (className instanceof SVGAnimatedString)
96 className = className.animVal;
97 var stringName = className.split(/\s/)[0].substring(5);
98 var prop = "innerHTML" in nodes[i] ? "innerHTML" : "textContent";
99 if(arguments.length > 0)
100 nodes[i][prop] = i18n.getMessage(stringName, arguments);
101 else
102 nodes[i][prop] = i18n.getMessage(stringName);
103 }
104 }
105
106 // Provides a more readable string of the current date and time
107 function i18n_timeDateStrings(when)
108 {
109 var d = new Date(when);
110 var timeString = d.toLocaleTimeString();
111
112 var now = new Date();
113 if (d.toDateString() == now.toDateString())
114 return [timeString];
115 else
116 return [timeString, d.toLocaleDateString()];
117 }
118
119 // Fill in the strings as soon as possible
120 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld