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

Side by Side Diff: i18n.js

Issue 6432313504169984: Issue 1663 - Various Firefox-related changes of the first-run page (Closed)
Patch Set: Created Jan. 6, 2015, 10:19 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
« no previous file with comments | « firstRun.js ('k') | messageResponder.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 var i18n; 18 // This variable should no longer be necessary once options.js in Chrome
19 // accesses ext.i18n directly.
20 var i18n = ext.i18n;
19 21
20 if (typeof ext != "undefined") 22 if (ext.i18n.noSpecialMessages)
21 { 23 {
22 i18n = ext.i18n; 24 ext.backgroundPage.sendMessage({
23 25 type: "app.get",
24 document.documentElement.lang = ext.i18n.getMessage("@@ui_locale").replace(/_/ g, "-"); 26 what: "localeInfo"
25 document.documentElement.dir = ext.i18n.getMessage("@@bidi_dir"); 27 }, function(localeInfo)
28 {
29 document.documentElement.lang = localeInfo.locale;
30 document.documentElement.dir = localeInfo.isRTL ? "rtl" : "ltr";
31 });
26 } 32 }
27 else 33 else
28 { 34 {
29 // Using Firefox' approach on i18n instead 35 document.documentElement.lang = ext.i18n.getMessage("@@ui_locale").replace(/_/ g, "-");
30 36 document.documentElement.dir = ext.i18n.getMessage("@@bidi_dir");
31 // Randomize URI to work around bug 719376
32 var pageName = location.pathname.replace(/.*\//, '').replace(/\..*?$/, '');
33 var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/ " + pageName +
34 ".properties?" + Math.random());
35
36 function getI18nMessage(key)
37 {
38 return {
39 "message": stringBundle.GetStringFromName(key)
40 };
41 }
42
43 i18n = (function()
44 {
45 function getText(message, args)
46 {
47 var text = message.message;
48 var placeholders = message.placeholders;
49
50 if (!args || !placeholders)
51 return text;
52
53 for (var key in placeholders)
54 {
55 var content = placeholders[key].content;
56 if (!content)
57 continue;
58
59 var index = parseInt(content.slice(1), 10);
60 if (isNaN(index))
61 continue;
62
63 var replacement = args[index - 1];
64 if (typeof replacement === "undefined")
65 continue;
66
67 text = text.split("$" + key + "$").join(replacement);
68 }
69 return text;
70 }
71
72 return {
73 getMessage: function(key, args)
74 {
75 try{
76 var message = getI18nMessage(key);
77 return getText(message, args);
78 }
79 catch(e)
80 {
81 Cu.reportError(e);
82 return "Missing translation: " + key;
83 }
84 }
85 };
86 })();
87
88 var Utils = require("utils").Utils;
89 document.documentElement.lang = Utils.appLocale;
90 document.documentElement.dir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr";
91 } 37 }
92 38
93 // Inserts i18n strings into matching elements. Any inner HTML already in the el ement is 39 // Inserts i18n strings into matching elements. Any inner HTML already in the el ement is
94 // parsed as JSON and used as parameters to substitute into placeholders in the i18n 40 // parsed as JSON and used as parameters to substitute into placeholders in the i18n
95 // message. 41 // message.
96 i18n.setElementText = function(element, stringName, arguments) 42 ext.i18n.setElementText = function(element, stringName, arguments)
97 { 43 {
98 function processString(str, element) 44 function processString(str, element)
99 { 45 {
100 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); 46 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str);
101 if (match) 47 if (match)
102 { 48 {
103 processString(match[1], element); 49 processString(match[1], element);
104 50
105 var e = document.createElement(match[2]); 51 var e = document.createElement(match[2]);
106 processString(match[3], e); 52 processString(match[3], e);
107 element.appendChild(e); 53 element.appendChild(e);
108 54
109 processString(match[4], element); 55 processString(match[4], element);
110 } 56 }
111 else 57 else
112 element.appendChild(document.createTextNode(str)); 58 element.appendChild(document.createTextNode(str));
113 } 59 }
114 60
115 while (element.lastChild) 61 while (element.lastChild)
116 element.removeChild(element.lastChild); 62 element.removeChild(element.lastChild);
117 processString(i18n.getMessage(stringName, arguments), element); 63 processString(ext.i18n.getMessage(stringName, arguments), element);
118 } 64 }
119 65
120 // Loads i18n strings 66 // Loads i18n strings
121 function loadI18nStrings() 67 function loadI18nStrings()
122 { 68 {
123 var nodes = document.querySelectorAll("[class^='i18n_']"); 69 var nodes = document.querySelectorAll("[class^='i18n_']");
124 for(var i = 0; i < nodes.length; i++) 70 for(var i = 0; i < nodes.length; i++)
125 { 71 {
126 var node = nodes[i]; 72 var node = nodes[i];
127 var arguments = JSON.parse("[" + node.textContent + "]"); 73 var arguments = JSON.parse("[" + node.textContent + "]");
128 if (arguments.length == 0) 74 if (arguments.length == 0)
129 arguments = null; 75 arguments = null;
130 76
131 var className = node.className; 77 var className = node.className;
132 if (className instanceof SVGAnimatedString) 78 if (className instanceof SVGAnimatedString)
133 className = className.animVal; 79 className = className.animVal;
134 var stringName = className.split(/\s/)[0].substring(5); 80 var stringName = className.split(/\s/)[0].substring(5);
135 81
136 i18n.setElementText(node, stringName, arguments); 82 ext.i18n.setElementText(node, stringName, arguments);
137 } 83 }
138 } 84 }
139 85
140 // Provides a more readable string of the current date and time 86 // Provides a more readable string of the current date and time
141 function i18n_timeDateStrings(when) 87 function i18n_timeDateStrings(when)
142 { 88 {
143 var d = new Date(when); 89 var d = new Date(when);
144 var timeString = d.toLocaleTimeString(); 90 var timeString = d.toLocaleTimeString();
145 91
146 var now = new Date(); 92 var now = new Date();
147 if (d.toDateString() == now.toDateString()) 93 if (d.toDateString() == now.toDateString())
148 return [timeString]; 94 return [timeString];
149 else 95 else
150 return [timeString, d.toLocaleDateString()]; 96 return [timeString, d.toLocaleDateString()];
151 } 97 }
152 98
153 // Fill in the strings as soon as possible 99 // Fill in the strings as soon as possible
154 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); 100 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW
« no previous file with comments | « firstRun.js ('k') | messageResponder.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld