| 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 internationalization |
| 8 * internationalization strings. | 8 * strings. |
| 9 * | |
| 10 * Example: | |
| 11 * behaviors: [ | |
| 12 * I18nBehavior, | |
| 13 * ], | |
| 14 */ | 9 */ |
| 15 | 10 |
| 16 /** @polymerBehavior */ | 11 /** @polymerBehavior */ |
| 17 var I18nBehavior = { | 12 var I18nBehavior = { |
| 13 properties: { |
| 14 /** |
| 15 * The language the UI is presented in. Used to signal dynamic language |
| 16 * change. |
| 17 */ |
| 18 locale: { |
| 19 type: String, |
| 20 value: '', |
| 21 }, |
| 22 }, |
| 23 |
| 18 /** | 24 /** |
| 19 * Returns a translated string where $1 to $9 are replaced by the given | 25 * Returns a translated string where $1 to $9 are replaced by the given |
| 20 * values. | 26 * values. |
| 21 * @param {string} id The ID of the string to translate. | 27 * @param {string} id The ID of the string to translate. |
| 22 * @param {...string} var_args Values to replace the placeholders $1 to $9 | 28 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
| 23 * in the string. | 29 * in the string. |
| 24 * @return {string} A translated, substituted string. | 30 * @return {string} A translated, substituted string. |
| 25 * @private | 31 * @private |
| 26 */ | 32 */ |
| 27 i18nRaw_: function(id, var_args) { | 33 i18nRaw_: function(id, var_args) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 53 * tags: (Array<string>|undefined)}} opts | 59 * tags: (Array<string>|undefined)}} opts |
| 54 */ | 60 */ |
| 55 i18nAdvanced: function(id, opts) { | 61 i18nAdvanced: function(id, opts) { |
| 56 var args = [id].concat(opts.substitutions || []); | 62 var args = [id].concat(opts.substitutions || []); |
| 57 var rawString = this.i18nRaw_.apply(this, args); | 63 var rawString = this.i18nRaw_.apply(this, args); |
| 58 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) | 64 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) |
| 59 .firstChild.innerHTML; | 65 .firstChild.innerHTML; |
| 60 }, | 66 }, |
| 61 | 67 |
| 62 /** | 68 /** |
| 69 * Similar to 'i18n', with an unused |locale| parameter used to trigger |
| 70 * updates when |this.locale| changes. |
| 71 * @param {string} locale The UI language used. |
| 72 * @param {string} id The ID of the string to translate. |
| 73 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
| 74 * in the string. |
| 75 * @return {string} A translated, sanitized, substituted string. |
| 76 */ |
| 77 i18nDynamic: function(locale, id, var_args) { |
| 78 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1)); |
| 79 }, |
| 80 |
| 81 /** |
| 63 * Returns true if a translation exists for |id|. | 82 * Returns true if a translation exists for |id|. |
| 64 * @param {string} id | 83 * @param {string} id |
| 65 * @return {boolean} | 84 * @return {boolean} |
| 66 */ | 85 */ |
| 67 i18nExists: function(id) { | 86 i18nExists: function(id) { |
| 68 return loadTimeData.valueExists(id); | 87 return loadTimeData.valueExists(id); |
| 69 }, | 88 }, |
| 89 |
| 90 /** |
| 91 * Call this when UI strings may have changed. This will send an update to any |
| 92 * data bindings to i18nDynamic(locale, ...). |
| 93 */ |
| 94 i18nUpdateLocale: function() { |
| 95 this.locale = loadTimeData.getString('language'); |
| 96 }, |
| 70 }; | 97 }; |
| 71 | 98 |
| 72 /** | 99 /** |
| 73 * TODO(stevenjb): Replace with an interface. b/24294625 | 100 * TODO(stevenjb): Replace with an interface. b/24294625 |
| 74 * @typedef {{ | 101 * @typedef {{ |
| 75 * i18n: function(string, ...string): string}}, | 102 * i18n: function(string, ...string): string}}, |
| 76 * i18nAdvanced: function({ | 103 * i18nAdvanced: function({ |
| 77 * substitutions: (Array<string>|undefined), | 104 * substitutions: (Array<string>|undefined), |
| 78 * attrs: (Object<function(Node, string):boolean>|undefined), | 105 * attrs: (Object<function(Node, string):boolean>|undefined), |
| 79 * tags: (Array<string>|undefined)}, opts), | 106 * tags: (Array<string>|undefined)}, opts), |
| 80 * i18nExists: function(string) | 107 * i18nExists: function(string) |
| 81 * }} | 108 * }} |
| 82 */ | 109 */ |
| 83 I18nBehavior.Proto; | 110 I18nBehavior.Proto; |
| OLD | NEW |