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

Delta Between Two Patch Sets: i18n.js

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

Powered by Google App Engine
This is Rietveld