OLD | NEW |
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; | |
19 | |
20 if (typeof ext != "undefined") | |
21 i18n = ext.i18n; | |
22 else if (typeof chrome != "undefined") | |
23 // TODO: This check only exist for backwards compatibility, while the Safari | |
24 // port isn't merged into the adblockpluschrome repo. So this branch should | |
25 // be removed when the Safari port was merged. | |
26 i18n = chrome.i18n; | |
27 else | |
28 { | |
29 // Using Firefox' approach on i18n instead | |
30 | |
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 | |
89 // Inserts i18n strings into matching elements. Any inner HTML already in the el
ement is | 18 // Inserts i18n strings into matching elements. Any inner HTML already in the el
ement is |
90 // parsed as JSON and used as parameters to substitute into placeholders in the
i18n | 19 // parsed as JSON and used as parameters to substitute into placeholders in the
i18n |
91 // message. | 20 // message. |
92 i18n.setElementText = function(element, stringName, arguments) | 21 ext.i18n.setElementText = function(element, stringName, arguments) |
93 { | 22 { |
94 function processString(str, element) | 23 function processString(str, element) |
95 { | 24 { |
96 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); | 25 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); |
97 if (match) | 26 if (match) |
98 { | 27 { |
99 processString(match[1], element); | 28 processString(match[1], element); |
100 | 29 |
101 var e = document.createElement(match[2]); | 30 var e = document.createElement(match[2]); |
102 processString(match[3], e); | 31 processString(match[3], e); |
103 element.appendChild(e); | 32 element.appendChild(e); |
104 | 33 |
105 processString(match[4], element); | 34 processString(match[4], element); |
106 } | 35 } |
107 else | 36 else |
108 element.appendChild(document.createTextNode(str)); | 37 element.appendChild(document.createTextNode(str)); |
109 } | 38 } |
110 | 39 |
111 while (element.lastChild) | 40 while (element.lastChild) |
112 element.removeChild(element.lastChild); | 41 element.removeChild(element.lastChild); |
113 processString(i18n.getMessage(stringName, arguments), element); | 42 processString(ext.i18n.getMessage(stringName, arguments), element); |
114 } | 43 } |
115 | 44 |
116 // Loads i18n strings | 45 // Loads i18n strings |
117 function loadI18nStrings() | 46 function loadI18nStrings() |
118 { | 47 { |
119 var nodes = document.querySelectorAll("[class^='i18n_']"); | 48 var nodes = document.querySelectorAll("[class^='i18n_']"); |
120 for(var i = 0; i < nodes.length; i++) | 49 for(var i = 0; i < nodes.length; i++) |
121 { | 50 { |
122 var node = nodes[i]; | 51 var node = nodes[i]; |
123 var arguments = JSON.parse("[" + node.textContent + "]"); | 52 var arguments = JSON.parse("[" + node.textContent + "]"); |
124 if (arguments.length == 0) | 53 if (arguments.length == 0) |
125 arguments = null; | 54 arguments = null; |
126 | 55 |
127 var className = node.className; | 56 var className = node.className; |
128 if (className instanceof SVGAnimatedString) | 57 if (className instanceof SVGAnimatedString) |
129 className = className.animVal; | 58 className = className.animVal; |
130 var stringName = className.split(/\s/)[0].substring(5); | 59 var stringName = className.split(/\s/)[0].substring(5); |
131 | 60 |
132 i18n.setElementText(node, stringName, arguments); | 61 ext.i18n.setElementText(node, stringName, arguments); |
133 } | 62 } |
134 } | 63 } |
135 | 64 |
136 // Provides a more readable string of the current date and time | 65 // Provides a more readable string of the current date and time |
137 function i18n_timeDateStrings(when) | 66 function i18n_timeDateStrings(when) |
138 { | 67 { |
139 var d = new Date(when); | 68 var d = new Date(when); |
140 var timeString = d.toLocaleTimeString(); | 69 var timeString = d.toLocaleTimeString(); |
141 | 70 |
142 var now = new Date(); | 71 var now = new Date(); |
143 if (d.toDateString() == now.toDateString()) | 72 if (d.toDateString() == now.toDateString()) |
144 return [timeString]; | 73 return [timeString]; |
145 else | 74 else |
146 return [timeString, d.toLocaleDateString()]; | 75 return [timeString, d.toLocaleDateString()]; |
147 } | 76 } |
148 | 77 |
149 // Fill in the strings as soon as possible | 78 // Fill in the strings as soon as possible |
150 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); | 79 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); |
OLD | NEW |