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

Side by Side Diff: i18n.js

Issue 4875452820750336: Issue 1663 - Moving first-run page to adblockplusui repository (Closed)
Patch Set: Created Dec. 16, 2014, 1:59 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') | locale/ar/firstRun.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 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
20 if (typeof ext != "undefined")
21 {
22 i18n = ext.i18n;
23
24 document.documentElement.lang = ext.i18n.getMessage("@@ui_locale").replace(/_/ g, "-");
25 document.documentElement.dir = ext.i18n.getMessage("@@bidi_dir");
26 }
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 var Utils = require("utils").Utils;
89 document.documentElement.lang = Utils.appLocale;
90 document.documentElement.dir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr";
91 }
92
93 // 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
95 // message.
96 i18n.setElementText = function(element, stringName, arguments)
97 {
98 function processString(str, element)
99 {
100 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str);
101 if (match)
102 {
103 processString(match[1], element);
104
105 var e = document.createElement(match[2]);
106 processString(match[3], e);
107 element.appendChild(e);
108
109 processString(match[4], element);
110 }
111 else
112 element.appendChild(document.createTextNode(str));
113 }
114
115 while (element.lastChild)
116 element.removeChild(element.lastChild);
117 processString(i18n.getMessage(stringName, arguments), element);
118 }
119
120 // Loads i18n strings
121 function loadI18nStrings()
122 {
123 var nodes = document.querySelectorAll("[class^='i18n_']");
124 for(var i = 0; i < nodes.length; i++)
125 {
126 var node = nodes[i];
127 var arguments = JSON.parse("[" + node.textContent + "]");
128 if (arguments.length == 0)
129 arguments = null;
130
131 var className = node.className;
132 if (className instanceof SVGAnimatedString)
133 className = className.animVal;
134 var stringName = className.split(/\s/)[0].substring(5);
135
136 i18n.setElementText(node, stringName, arguments);
137 }
138 }
139
140 // Provides a more readable string of the current date and time
141 function i18n_timeDateStrings(when)
142 {
143 var d = new Date(when);
144 var timeString = d.toLocaleTimeString();
145
146 var now = new Date();
147 if (d.toDateString() == now.toDateString())
148 return [timeString];
149 else
150 return [timeString, d.toLocaleDateString()];
151 }
152
153 // Fill in the strings as soon as possible
154 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW
« no previous file with comments | « firstRun.js ('k') | locale/ar/firstRun.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld