| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * 'I18nBehavior' is a behavior to mix in loading of | 7 * 'I18nBehavior' is a behavior to mix in loading of |
| 8 * internationalization strings. | 8 * internationalization strings. |
| 9 * | 9 * |
| 10 * Example: | 10 * Example: |
| 11 * behaviors: [ | 11 * behaviors: [ |
| 12 * I18nBehavior, | 12 * I18nBehavior, |
| 13 * ], | 13 * ], |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 /** @polymerBehavior */ | 16 /** @polymerBehavior */ |
| 17 var I18nBehavior = { | 17 var I18nBehavior = { |
| 18 /** | 18 /** |
| 19 * Returns a translated string where $1 to $9 are replaced by the given |
| 20 * values. |
| 19 * @param {string} id The ID of the string to translate. | 21 * @param {string} id The ID of the string to translate. |
| 20 * @param {...string} var_args Placeholders required by the string ($0-9). | 22 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
| 23 * in the string. |
| 21 * @return {string} A translated, substituted string. | 24 * @return {string} A translated, substituted string. |
| 22 */ | 25 */ |
| 23 i18n: function(id, var_args) { | 26 i18nRaw: function(id, var_args) { |
| 24 return arguments.length == 1 ? loadTimeData.getString(id) : | 27 return arguments.length == 1 ? loadTimeData.getString(id) : |
| 25 loadTimeData.getStringF.apply(loadTimeData, arguments); | 28 loadTimeData.getStringF.apply(loadTimeData, arguments); |
| 26 }, | 29 }, |
| 30 |
| 31 /** |
| 32 * Returns a translated string where $1 to $9 are replaced by the given |
| 33 * values. Also sanitizes the output to filter out dangerous HTML/JS. |
| 34 * @param {string} id The ID of the string to translate. |
| 35 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
| 36 * in the string. |
| 37 * @return {string} A translated, sanitized, substituted string. |
| 38 */ |
| 39 i18n: function(id, var_args) { |
| 40 var rawString = this.i18nRaw.apply(this, arguments); |
| 41 return parseHtmlSubset('<b>' + rawString + '</b>').firstChild.innerHTML; |
| 42 }, |
| 43 |
| 44 /** |
| 45 * Similar to 'i18n', returns a translated, sanitized, substituted string. |
| 46 * It receives the string ID and a dictionary containing the substitutions |
| 47 * as well as optional additional allowed tags and attributes. |
| 48 * @param {string} id The ID of the string to translate. |
| 49 * @param {{substitutions: (Array<string>|undefined), |
| 50 * attrs: (Object<function(Node, string):boolean>|undefined), |
| 51 * tags: (Array<string>|undefined)}} opts |
| 52 */ |
| 53 i18nAdvanced: function(id, opts) { |
| 54 var args = [id].concat(opts.substitutions || []); |
| 55 var rawString = this.i18nRaw.apply(this, args); |
| 56 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) |
| 57 .firstChild.innerHTML; |
| 58 } |
| 27 }; | 59 }; |
| OLD | NEW |