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 | 19 * Returns a translated string where $1 to $9 are replaced by the given |
20 * values. | 20 * values. |
21 * @param {string} id The ID of the string to translate. | 21 * @param {string} id The ID of the string to translate. |
22 * @param {...string} var_args Values to replace the placeholders $1 to $9 | 22 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
23 * in the string. | 23 * in the string. |
24 * @return {string} A translated, substituted string. | 24 * @return {string} A translated, substituted string. |
25 * @private | 25 * @private |
26 */ | 26 */ |
27 i18nRaw_: function(id, var_args) { | 27 i18nRaw_: function(id, var_args) { |
28 return arguments.length == 1 ? loadTimeData.getString(id) : | 28 return arguments.length == 1 ? |
| 29 loadTimeData.getString(id) : |
29 loadTimeData.getStringF.apply(loadTimeData, arguments); | 30 loadTimeData.getStringF.apply(loadTimeData, arguments); |
30 }, | 31 }, |
31 | 32 |
32 /** | 33 /** |
33 * Returns a translated string where $1 to $9 are replaced by the given | 34 * Returns a translated string where $1 to $9 are replaced by the given |
34 * values. Also sanitizes the output to filter out dangerous HTML/JS. | 35 * values. Also sanitizes the output to filter out dangerous HTML/JS. |
35 * @param {string} id The ID of the string to translate. | 36 * @param {string} id The ID of the string to translate. |
36 * @param {...string} var_args Values to replace the placeholders $1 to $9 | 37 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
37 * in the string. | 38 * in the string. |
38 * @return {string} A translated, sanitized, substituted string. | 39 * @return {string} A translated, sanitized, substituted string. |
(...skipping 17 matching lines...) Expand all Loading... |
56 var rawString = this.i18nRaw_.apply(this, args); | 57 var rawString = this.i18nRaw_.apply(this, args); |
57 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) | 58 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) |
58 .firstChild.innerHTML; | 59 .firstChild.innerHTML; |
59 }, | 60 }, |
60 | 61 |
61 /** | 62 /** |
62 * Returns true if a translation exists for |id|. | 63 * Returns true if a translation exists for |id|. |
63 * @param {string} id | 64 * @param {string} id |
64 * @return {boolean} | 65 * @return {boolean} |
65 */ | 66 */ |
66 i18nExists: function(id) { | 67 i18nExists: function(id) { return loadTimeData.valueExists(id); }, |
67 return loadTimeData.valueExists(id); | |
68 }, | |
69 }; | 68 }; |
70 | 69 |
71 /** | 70 /** |
72 * TODO(stevenjb): Replace with an interface. b/24294625 | 71 * TODO(stevenjb): Replace with an interface. b/24294625 |
73 * @typedef {{ | 72 * @typedef {{ |
74 * i18n: function(string, ...string): string}}, | 73 * i18n: function(string, ...string): string}}, |
75 * i18nAdvanced: function({ | 74 * i18nAdvanced: function({ |
76 * substitutions: (Array<string>|undefined), | 75 * substitutions: (Array<string>|undefined), |
77 * attrs: (Object<function(Node, string):boolean>|undefined), | 76 * attrs: (Object<function(Node, string):boolean>|undefined), |
78 * tags: (Array<string>|undefined)}, opts), | 77 * tags: (Array<string>|undefined)}, opts), |
79 * i18nExists: function(string) | 78 * i18nExists: function(string) |
80 * }} | 79 * }} |
81 */ | 80 */ |
82 I18nBehavior.Proto; | 81 I18nBehavior.Proto; |
OLD | NEW |