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: Addressed comments Created Jan. 7, 2015, 4:31 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.getMessage("@@ui_locale"))
21 { 23 {
22 i18n = ext.i18n;
23
24 document.documentElement.lang = ext.i18n.getMessage("@@ui_locale").replace(/_/ g, "-"); 24 document.documentElement.lang = ext.i18n.getMessage("@@ui_locale").replace(/_/ g, "-");
25 document.documentElement.dir = ext.i18n.getMessage("@@bidi_dir"); 25 document.documentElement.dir = ext.i18n.getMessage("@@bidi_dir");
26 } 26 }
27 else 27 else
28 { 28 {
29 // Using Firefox' approach on i18n instead 29 // Getting UI locale cannot be done synchronously on Firefox, requires
30 30 // messaging the background page.
31 // Randomize URI to work around bug 719376 31 ext.backgroundPage.sendMessage({
32 var pageName = location.pathname.replace(/.*\//, '').replace(/\..*?$/, ''); 32 type: "app.get",
33 var stringBundle = Services.strings.createBundle("chrome://adblockplus/locale/ " + pageName + 33 what: "localeInfo"
34 ".properties?" + Math.random()); 34 }, function(localeInfo)
35
36 function getI18nMessage(key)
37 { 35 {
38 return { 36 document.documentElement.lang = localeInfo.locale;
39 "message": stringBundle.GetStringFromName(key) 37 document.documentElement.dir = localeInfo.isRTL ? "rtl" : "ltr";
40 }; 38 });
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 } 39 }
92 40
93 // Inserts i18n strings into matching elements. Any inner HTML already in the el ement is 41 // 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 42 // parsed as JSON and used as parameters to substitute into placeholders in the i18n
95 // message. 43 // message.
96 i18n.setElementText = function(element, stringName, arguments) 44 ext.i18n.setElementText = function(element, stringName, arguments)
97 { 45 {
98 function processString(str, element) 46 function processString(str, element)
99 { 47 {
100 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); 48 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str);
101 if (match) 49 if (match)
102 { 50 {
103 processString(match[1], element); 51 processString(match[1], element);
104 52
105 var e = document.createElement(match[2]); 53 var e = document.createElement(match[2]);
106 processString(match[3], e); 54 processString(match[3], e);
107 element.appendChild(e); 55 element.appendChild(e);
108 56
109 processString(match[4], element); 57 processString(match[4], element);
110 } 58 }
111 else 59 else
112 element.appendChild(document.createTextNode(str)); 60 element.appendChild(document.createTextNode(str));
113 } 61 }
114 62
115 while (element.lastChild) 63 while (element.lastChild)
116 element.removeChild(element.lastChild); 64 element.removeChild(element.lastChild);
117 processString(i18n.getMessage(stringName, arguments), element); 65 processString(ext.i18n.getMessage(stringName, arguments), element);
118 } 66 }
119 67
120 // Loads i18n strings 68 // Loads i18n strings
121 function loadI18nStrings() 69 function loadI18nStrings()
122 { 70 {
123 var nodes = document.querySelectorAll("[class^='i18n_']"); 71 var nodes = document.querySelectorAll("[class^='i18n_']");
124 for(var i = 0; i < nodes.length; i++) 72 for(var i = 0; i < nodes.length; i++)
125 { 73 {
126 var node = nodes[i]; 74 var node = nodes[i];
127 var arguments = JSON.parse("[" + node.textContent + "]"); 75 var arguments = JSON.parse("[" + node.textContent + "]");
128 if (arguments.length == 0) 76 if (arguments.length == 0)
129 arguments = null; 77 arguments = null;
130 78
131 var className = node.className; 79 var className = node.className;
132 if (className instanceof SVGAnimatedString) 80 if (className instanceof SVGAnimatedString)
133 className = className.animVal; 81 className = className.animVal;
134 var stringName = className.split(/\s/)[0].substring(5); 82 var stringName = className.split(/\s/)[0].substring(5);
135 83
136 i18n.setElementText(node, stringName, arguments); 84 ext.i18n.setElementText(node, stringName, arguments);
137 } 85 }
138 } 86 }
139 87
140 // Provides a more readable string of the current date and time 88 // Provides a more readable string of the current date and time
141 function i18n_timeDateStrings(when) 89 function i18n_timeDateStrings(when)
142 { 90 {
143 var d = new Date(when); 91 var d = new Date(when);
144 var timeString = d.toLocaleTimeString(); 92 var timeString = d.toLocaleTimeString();
145 93
146 var now = new Date(); 94 var now = new Date();
147 if (d.toDateString() == now.toDateString()) 95 if (d.toDateString() == now.toDateString())
148 return [timeString]; 96 return [timeString];
149 else 97 else
150 return [timeString, d.toLocaleDateString()]; 98 return [timeString, d.toLocaleDateString()];
151 } 99 }
152 100
153 // Fill in the strings as soon as possible 101 // Fill in the strings as soon as possible
154 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); 102 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