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

Side by Side Diff: chrome/content/ui/i18n.js

Issue 11790013: Expose string insertion functionality from i18n.js to public (Closed)
Patch Set: Created Sept. 20, 2013, 3:46 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 catch(e) 75 catch(e)
76 { 76 {
77 Cu.reportError(e); 77 Cu.reportError(e);
78 return "Missing translation: " + key; 78 return "Missing translation: " + key;
79 } 79 }
80 } 80 }
81 }; 81 };
82 })(); 82 })();
83 } 83 }
84 84
85 // Loads and inserts i18n strings into matching elements. Any inner HTML already in the 85 // Inserts i18n strings into matching elements. Any inner HTML already in the el ement is
86 // element is parsed as JSON and used as parameters to substitute into placehold ers in the 86 // parsed as JSON and used as parameters to substitute into placeholders in the i18n
87 // i18n message. 87 // message.
88 function loadI18nStrings() 88 var setElementText = i18n.setElementText = function(element, stringName, argumen ts)
Wladimir Palant 2013/09/24 12:45:46 Given that this is loaded into global context I su
89 { 89 {
90 function processString(str, element) 90 function processString(str, element)
91 { 91 {
92 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str); 92 var match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(str);
93 if (match) 93 if (match)
94 { 94 {
95 processString(match[1], element); 95 processString(match[1], element);
96 96
97 var e = document.createElement(match[2]); 97 var e = document.createElement(match[2]);
98 processString(match[3], e); 98 processString(match[3], e);
99 element.appendChild(e); 99 element.appendChild(e);
100 100
101 processString(match[4], element); 101 processString(match[4], element);
102 } 102 }
103 else 103 else
104 element.appendChild(document.createTextNode(str)); 104 element.appendChild(document.createTextNode(str));
105 } 105 }
106 106
107 while (element.lastChild)
108 element.removeChild(element.lastChild);
109 processString(i18n.getMessage(stringName, arguments), element);
110 }
111
112 // Loads i18n strings
113 function loadI18nStrings()
114 {
107 var nodes = document.querySelectorAll("[class^='i18n_']"); 115 var nodes = document.querySelectorAll("[class^='i18n_']");
108 for(var i = 0; i < nodes.length; i++) 116 for(var i = 0; i < nodes.length; i++)
109 { 117 {
110 var node = nodes[i]; 118 var node = nodes[i];
111 var arguments = JSON.parse("[" + node.textContent + "]"); 119 var arguments = JSON.parse("[" + node.textContent + "]");
112 if (arguments.length == 0) 120 if (arguments.length == 0)
113 arguments = null; 121 arguments = null;
114 122
115 var className = node.className; 123 var className = node.className;
116 if (className instanceof SVGAnimatedString) 124 if (className instanceof SVGAnimatedString)
117 className = className.animVal; 125 className = className.animVal;
118 var stringName = className.split(/\s/)[0].substring(5); 126 var stringName = className.split(/\s/)[0].substring(5);
119 127
120 while (node.lastChild) 128 setElementText(node, stringName, arguments);
121 node.removeChild(node.lastChild);
122 processString(i18n.getMessage(stringName, arguments), node);
123 } 129 }
124 } 130 }
125 131
126 // Provides a more readable string of the current date and time 132 // Provides a more readable string of the current date and time
127 function i18n_timeDateStrings(when) 133 function i18n_timeDateStrings(when)
128 { 134 {
129 var d = new Date(when); 135 var d = new Date(when);
130 var timeString = d.toLocaleTimeString(); 136 var timeString = d.toLocaleTimeString();
131 137
132 var now = new Date(); 138 var now = new Date();
133 if (d.toDateString() == now.toDateString()) 139 if (d.toDateString() == now.toDateString())
134 return [timeString]; 140 return [timeString];
135 else 141 else
136 return [timeString, d.toLocaleDateString()]; 142 return [timeString, d.toLocaleDateString()];
137 } 143 }
138 144
139 // Fill in the strings as soon as possible 145 // Fill in the strings as soon as possible
140 window.addEventListener("DOMContentLoaded", loadI18nStrings, true); 146 window.addEventListener("DOMContentLoaded", loadI18nStrings, true);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld