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

Side by Side Diff: i18n.js

Issue 8560083: adblockplusopera: Port UI code from Chrome (Closed)
Patch Set: Created Oct. 19, 2012, 4:04 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 | « help/help.js ('k') | icons/abp-128.png » ('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 Source Code is subject to the terms of the Mozilla Public License
3 * version 2.0 (the "License"). You can obtain a copy of the License at
4 * http://mozilla.org/MPL/2.0/.
5 */
6
7 var i18n = (function()
8 {
9 function getText(message, args)
10 {
11 var text = message.message;
12 var placeholders = message.placeholders;
13
14 if (!args || !placeholders)
15 return text;
16
17 for (var key in placeholders)
18 {
19 var content = placeholders[key].content;
20 if (!content)
21 continue;
22
23 var index = parseInt(content.slice(1), 10);
24 if (isNaN(index))
25 continue;
26
27 var replacement = args[index - 1];
28 if (typeof replacement === "undefined")
29 continue;
30
31 text = text.split("$" + key + "$").join(replacement);
32 }
33 return text;
34 }
35
36 return {
37 getMessage: function(key, args)
38 {
39 var messages = opera.extension.bgProcess.i18nMessages;
40 var message = messages[key];
41 if (!message)
42 return "Missing translation: " + key;
43 return getText(message, args);
44 }
45 };
46 })();
47
48 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the
49 // element is parsed as JSON and used as parameters to substitute into placehold ers in the
50 // i18n message.
51 function loadI18nStrings() {
52 var nodes = document.querySelectorAll("[class^='i18n_']");
53 for(var i = 0; i < nodes.length; i++) {
54 var arguments = JSON.parse("[" + nodes[i].textContent + "]");
55 var className = nodes[i].className;
56 var stringName = className.split(/\s/)[0].substring(5);
57 var prop = "innerHTML" in nodes[i] ? "innerHTML" : "textContent";
58 if(arguments.length > 0)
59 nodes[i][prop] = i18n.getMessage(stringName, arguments);
60 else
61 nodes[i][prop] = i18n.getMessage(stringName);
62 }
63 }
64
65 // Provides a more readable string of the current date and time
66 function i18n_timeDateStrings(when) {
67 var d = new Date(when);
68 var timeString = d.toLocaleTimeString();
69
70 var now = new Date();
71 if (d.toDateString() == now.toDateString())
72 return [timeString];
73 else
74 return [timeString, d.toLocaleDateString()];
75 }
76
77 // Fill in the strings as soon as possible
78 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW
« no previous file with comments | « help/help.js ('k') | icons/abp-128.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld