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

Delta Between Two Patch Sets: safari/ext/common.js

Issue 6326608939974656: Issue 1502 - Cache translation catalogs on Safari (Closed)
Left Patch Set: Created Oct. 28, 2014, 6:50 p.m.
Right Patch Set: Renamed "source" variable to avoid confusion Created Oct. 29, 2014, 7:58 a.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 | « no previous file | no next file » | 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 file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 candidates.push(defaultLocale); 95 candidates.push(defaultLocale);
96 96
97 return candidates; 97 return candidates;
98 }; 98 };
99 99
100 var locales = getLocaleCandidates(); 100 var locales = getLocaleCandidates();
101 var catalog = {__proto__: null, "@@ui_locale": [locales[0], []]}; 101 var catalog = {__proto__: null, "@@ui_locale": [locales[0], []]};
102 102
103 var replacePlaceholder = function(text, placeholder, content) 103 var replacePlaceholder = function(text, placeholder, content)
104 { 104 {
105 return text.split("$" + placeholder + "$").join(content || ""); 105 return text.split("$" + placeholder + "$").join(content || "");
Wladimir Palant 2014/10/30 23:00:44 Why not keep the more obvious text.replace(...)? A
Sebastian Noack 2014/10/30 23:53:01 Because .replace() with a string as first argument
106 }; 106 };
107 107
108 var parseMessage = function(source) 108 var parseMessage = function(rawMessage)
109 { 109 {
110 var text = source.message; 110 var text = rawMessage.message;
111 var placeholders = []; 111 var placeholders = [];
112 112
113 for (var placeholder in source.placeholders) 113 for (var placeholder in rawMessage.placeholders)
Wladimir Palant 2014/10/30 23:00:44 I didn't expect it but apparently one can iterate
Sebastian Noack 2014/10/30 23:53:01 You can. Iterating over null (or undefined) has th
114 { 114 {
115 var content = source.placeholders[placeholder].content; 115 var content = rawMessage.placeholders[placeholder].content;
116 116
117 if (/^\$\d+$/.test(content)) 117 if (/^\$\d+$/.test(content))
118 placeholders[parseInt(content.substr(1)) - 1] = placeholder; 118 placeholders[parseInt(content.substr(1)) - 1] = placeholder;
Wladimir Palant 2014/10/30 23:00:44 Doing both regular expressions and string manipula
Sebastian Noack 2014/10/30 23:53:01 Neither the old code, nor most other calls to pars
119 else 119 else
120 text = replacePlaceholder(text, placeholder, content); 120 text = replacePlaceholder(text, placeholder, content);
121 } 121 }
122 122
123 return [text, placeholders]; 123 return [text, placeholders];
124 }; 124 };
125 125
126 var readCatalog = function(locale) 126 var readCatalog = function(locale)
127 { 127 {
128 var xhr = new XMLHttpRequest(); 128 var xhr = new XMLHttpRequest();
129
130 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages .json", false); 129 xhr.open("GET", safari.extension.baseURI + "_locales/" + locale + "/messages .json", false);
131 130
132 try 131 try
133 { 132 {
134 xhr.send(); 133 xhr.send();
135 } 134 }
136 catch (e) 135 catch (e)
137 { 136 {
138 return; 137 return;
139 } 138 }
140 139
141 if (xhr.status != 200 && xhr.status != 0) 140 if (xhr.status != 200 && xhr.status != 0)
142 return; 141 return;
143 142
144 var source = JSON.parse(xhr.responseText); 143 var rawCatalog = JSON.parse(xhr.responseText);
kzar 2014/10/28 19:12:16 I found it confusing that this variable is called
Sebastian Noack 2014/10/28 19:35:53 I could argue that this is a different context and
kzar 2014/10/28 22:05:29 Perhaps the source variable here could be renamed
Sebastian Noack 2014/10/29 08:08:54 Not really consistent. Since the variable storing
kzar 2014/10/30 12:40:15 Cool that makes more sense to me now.
145 for (var msgId in source) 144 for (var msgId in rawCatalog)
146 { 145 {
147 if (!(msgId in catalog)) 146 if (!(msgId in catalog))
148 catalog[msgId] = parseMessage(source[msgId]); 147 catalog[msgId] = parseMessage(rawCatalog[msgId]);
149 } 148 }
150 }; 149 };
151 150
152 ext.i18n = { 151 ext.i18n = {
153 getMessage: function(msgId, substitutions) 152 getMessage: function(msgId, substitutions)
154 { 153 {
155 while (true) 154 while (true)
156 { 155 {
157 var message = catalog[msgId]; 156 var message = catalog[msgId];
158 if (message) 157 if (message)
159 { 158 {
160 var [text, placeholders] = message; 159 var [text, placeholders] = message;
kzar 2014/10/28 19:12:16 I've never seen this syntax in JS before, sure it'
Sebastian Noack 2014/10/28 19:35:53 It's called destructuring assignment, and is a new
kzar 2014/10/28 22:05:29 I guessed you were somehow right :p (Understand ab
161 160
162 if (!(substitutions instanceof Array)) 161 if (!(substitutions instanceof Array))
163 substitutions = [substitutions]; 162 substitutions = [substitutions];
164 163
165 for (var i = 0; i < placeholders.length; i++) 164 for (var i = 0; i < placeholders.length; i++)
166 text = replacePlaceholder(text, placeholders[i], substitutions[i]); 165 text = replacePlaceholder(text, placeholders[i], substitutions[i]);
167 166
168 return text; 167 return text;
169 } 168 }
170 169
171 if (locales.length == 0) 170 if (locales.length == 0)
172 return ""; 171 return "";
173 172
174 readCatalog(locales.shift()); 173 readCatalog(locales.shift());
175 } 174 }
176 } 175 }
177 }; 176 };
178 177
179 178
180 /* Utils */ 179 /* Utils */
181 180
182 ext.getURL = function(path) 181 ext.getURL = function(path)
183 { 182 {
184 return safari.extension.baseURI + path; 183 return safari.extension.baseURI + path;
185 }; 184 };
186 })(); 185 })();
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld