Révision 5587145e
Ajouté par Assos Assos il y a presque 7 ans
drupal7/misc/drupal.js | ||
---|---|---|
168 | 168 |
Drupal.formatString = function(str, args) { |
169 | 169 |
// Transform arguments before inserting them. |
170 | 170 |
for (var key in args) { |
171 |
switch (key.charAt(0)) { |
|
172 |
// Escaped only. |
|
173 |
case '@': |
|
174 |
args[key] = Drupal.checkPlain(args[key]); |
|
175 |
break; |
|
176 |
// Pass-through. |
|
177 |
case '!': |
|
178 |
break; |
|
179 |
// Escaped and placeholder. |
|
180 |
case '%': |
|
181 |
default: |
|
182 |
args[key] = Drupal.theme('placeholder', args[key]); |
|
183 |
break; |
|
171 |
if (args.hasOwnProperty(key)) { |
|
172 |
switch (key.charAt(0)) { |
|
173 |
// Escaped only. |
|
174 |
case '@': |
|
175 |
args[key] = Drupal.checkPlain(args[key]); |
|
176 |
break; |
|
177 |
// Pass-through. |
|
178 |
case '!': |
|
179 |
break; |
|
180 |
// Escaped and placeholder. |
|
181 |
default: |
|
182 |
args[key] = Drupal.theme('placeholder', args[key]); |
|
183 |
break; |
|
184 |
} |
|
184 | 185 |
} |
185 |
str = str.replace(key, args[key]); |
|
186 | 186 |
} |
187 |
return str; |
|
187 |
|
|
188 |
return Drupal.stringReplace(str, args, null); |
|
189 |
}; |
|
190 |
|
|
191 |
/** |
|
192 |
* Replace substring. |
|
193 |
* |
|
194 |
* The longest keys will be tried first. Once a substring has been replaced, |
|
195 |
* its new value will not be searched again. |
|
196 |
* |
|
197 |
* @param {String} str |
|
198 |
* A string with placeholders. |
|
199 |
* @param {Object} args |
|
200 |
* Key-value pairs. |
|
201 |
* @param {Array|null} keys |
|
202 |
* Array of keys from the "args". Internal use only. |
|
203 |
* |
|
204 |
* @return {String} |
|
205 |
* Returns the replaced string. |
|
206 |
*/ |
|
207 |
Drupal.stringReplace = function (str, args, keys) { |
|
208 |
if (str.length === 0) { |
|
209 |
return str; |
|
210 |
} |
|
211 |
|
|
212 |
// If the array of keys is not passed then collect the keys from the args. |
|
213 |
if (!$.isArray(keys)) { |
|
214 |
keys = []; |
|
215 |
for (var k in args) { |
|
216 |
if (args.hasOwnProperty(k)) { |
|
217 |
keys.push(k); |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
// Order the keys by the character length. The shortest one is the first. |
|
222 |
keys.sort(function (a, b) { return a.length - b.length; }); |
|
223 |
} |
|
224 |
|
|
225 |
if (keys.length === 0) { |
|
226 |
return str; |
|
227 |
} |
|
228 |
|
|
229 |
// Take next longest one from the end. |
|
230 |
var key = keys.pop(); |
|
231 |
var fragments = str.split(key); |
|
232 |
|
|
233 |
if (keys.length) { |
|
234 |
for (var i = 0; i < fragments.length; i++) { |
|
235 |
// Process each fragment with a copy of remaining keys. |
|
236 |
fragments[i] = Drupal.stringReplace(fragments[i], args, keys.slice(0)); |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
return fragments.join(args[key]); |
|
188 | 241 |
}; |
189 | 242 |
|
190 | 243 |
/** |
... | ... | |
251 | 304 |
* A translated string. |
252 | 305 |
*/ |
253 | 306 |
Drupal.formatPlural = function (count, singular, plural, args, options) { |
254 |
var args = args || {};
|
|
307 |
args = args || {}; |
|
255 | 308 |
args['@count'] = count; |
256 | 309 |
// Determine the index of the plural form. |
257 | 310 |
var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1); |
Formats disponibles : Unified diff
Update to 7.56