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: Applied proposed changes (except Chrome-specific utils.js due to uncertainty) Created May 27, 2013, 12:58 p.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 {
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("/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 return "Missing translation: " + key;
Wladimir Palant 2013/05/27 14:10:37 Cu.reportError(e) before returning please - a miss
Thomas Greiner 2013/05/27 16:39:16 Done.
78 }
79 }
80 };
81 })();
82 }
83
84 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the
85 // element is parsed as JSON and used as parameters to substitute into placehold ers in the
86 // i18n message.
87 function loadI18nStrings()
88 {
89 var nodes = document.querySelectorAll("[class^='i18n_']");
90 for(var i = 0; i < nodes.length; i++)
91 {
92 var arguments = JSON.parse("[" + nodes[i].textContent + "]");
93 var className = nodes[i].className;
94 if (className instanceof SVGAnimatedString)
95 className = className.animVal;
96 var stringName = className.split(/\s/)[0].substring(5);
97 var prop = "innerHTML" in nodes[i] ? "innerHTML" : "textContent";
98 if(arguments.length > 0)
99 nodes[i][prop] = i18n.getMessage(stringName, arguments);
100 else
101 nodes[i][prop] = i18n.getMessage(stringName);
102 }
103 }
104
105 // Provides a more readable string of the current date and time
106 function i18n_timeDateStrings(when)
107 {
108 var d = new Date(when);
109 var timeString = d.toLocaleTimeString();
110
111 var now = new Date();
112 if (d.toDateString() == now.toDateString())
113 return [timeString];
114 else
115 return [timeString, d.toLocaleDateString()];
116 }
117
118 // Fill in the strings as soon as possible
119 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW

Powered by Google App Engine
This is Rietveld