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. 12, 2012, 1:11 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
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;
Wladimir Palant 2012/10/12 15:04:02 "Uncaught exception: message is undefined". You mi
Felix Dahlke 2012/10/16 14:20:01 Done.
12
13 if (!args)
Wladimir Palant 2012/10/12 15:04:02 if (!args || !message.placeholders) I think, other
Felix Dahlke 2012/10/16 14:20:01 You're right, done.
14 return text;
15
16 var placeholders = message.placeholders;
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));
Wladimir Palant 2012/10/12 15:04:02 parseInt(..., 10) please.
Felix Dahlke 2012/10/16 14:20:01 Not again! :) Done.
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 = getText(messages[key], args);
41 if (!message)
42 return "Missing translation: " + key;
Wladimir Palant 2012/10/12 15:04:02 Actually, missing translations are currently quite
Felix Dahlke 2012/10/16 14:20:01 Done, see my changes and comments to background.js
43 return message;
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 function i18n_time(h, m) {
66 var locale = i18n.getMessage("@@ui_locale");
Wladimir Palant 2012/10/12 15:04:02 @@ui_locale is a special string that isn't part of
Felix Dahlke 2012/10/16 14:20:01 Done.
67 if(m < 10)
68 m = "0" + m;
69 if(locale == "fr") {
70 return h + "h" + m;
71 } else {
72 var ampm = "a.m.";
73 if(h >= 12) {
74 h -= 12;
75 ampm = "p.m.";
76 }
77 if(h == 0)
78 h = 12;
79 return(h + ":" + m + " " + ampm);
80 }
81 }
82
83 // Provides a more readable string of the current date and time
84 function i18n_timeDateStrings(when) {
85 var d = new Date(when);
86 var timeString = d.toLocaleTimeString();
87
88 var now = new Date();
89 if (d.toDateString() == now.toDateString())
90 return [timeString];
91 else
92 return [timeString, d.toLocaleDateString()];
93 }
94
95 // Fill in the strings as soon as possible
96 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW

Powered by Google App Engine
This is Rietveld