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 internationalization | 7 * 'I18nBehavior' is a behavior to mix in loading of internationalization |
8 * strings. | 8 * strings. |
9 */ | 9 */ |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... | |
32 */ | 32 */ |
33 i18nRaw_: function(id, var_args) { | 33 i18nRaw_: function(id, var_args) { |
34 return arguments.length == 1 ? | 34 return arguments.length == 1 ? |
35 loadTimeData.getString(id) : | 35 loadTimeData.getString(id) : |
36 loadTimeData.getStringF.apply(loadTimeData, arguments); | 36 loadTimeData.getStringF.apply(loadTimeData, arguments); |
37 }, | 37 }, |
38 | 38 |
39 /** | 39 /** |
40 * Returns a translated string where $1 to $9 are replaced by the given | 40 * Returns a translated string where $1 to $9 are replaced by the given |
41 * values. Also sanitizes the output to filter out dangerous HTML/JS. | 41 * values. Also sanitizes the output to filter out dangerous HTML/JS. |
42 * Use with Polymer bindings that are *not* inner-h-t-m-l. | |
42 * @param {string} id The ID of the string to translate. | 43 * @param {string} id The ID of the string to translate. |
43 * @param {...string} var_args Values to replace the placeholders $1 to $9 | 44 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
44 * in the string. | 45 * in the string. |
45 * @return {string} A translated, sanitized, substituted string. | 46 * @return {string} A translated, sanitized, substituted string. |
46 */ | 47 */ |
47 i18n: function(id, var_args) { | 48 i18n: function(id, var_args) { |
48 var rawString = this.i18nRaw_.apply(this, arguments); | 49 var rawString = this.i18nRaw_.apply(this, arguments); |
49 var htmlStr = | 50 return parseHtmlSubset('<b>' + rawString + '</b>').firstChild.textContent; |
50 parseHtmlSubset('<b>' + rawString + '</b>').firstChild.innerHTML; | |
51 // TODO(dschuyler): use textContent rather than innerHTML; remove replace(). | |
52 return htmlStr.replace(' ', '\u00a0'); | |
53 }, | 51 }, |
54 | 52 |
55 /** | 53 /** |
56 * Similar to 'i18n', returns a translated, sanitized, substituted string. | 54 * Similar to 'i18n', returns a translated, sanitized, substituted string. |
57 * It receives the string ID and a dictionary containing the substitutions | 55 * It receives the string ID and a dictionary containing the substitutions |
58 * as well as optional additional allowed tags and attributes. | 56 * as well as optional additional allowed tags and attributes. Use with |
57 * Polymer bindings that are inner-h-t-m-l, for example. | |
59 * @param {string} id The ID of the string to translate. | 58 * @param {string} id The ID of the string to translate. |
60 * @param {{substitutions: (Array<string>|undefined), | 59 * @param {I18nAdvancedOpts=} opts |
61 * attrs: (Object<function(Node, string):boolean>|undefined), | 60 * @return {string} |
62 * tags: (Array<string>|undefined)}} opts | |
63 */ | 61 */ |
64 i18nAdvanced: function(id, opts) { | 62 i18nAdvanced: function(id, opts) { |
63 if (typeof opts === 'undefined') | |
64 opts = {}; | |
Dan Beam
2017/06/08 23:21:41
opts = opts || {};
dschuyler
2017/06/09 00:13:39
Done.
| |
65 var args = [id].concat(opts.substitutions || []); | 65 var args = [id].concat(opts.substitutions || []); |
66 var rawString = this.i18nRaw_.apply(this, args); | 66 var rawString = this.i18nRaw_.apply(this, args); |
67 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) | 67 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) |
68 .firstChild.innerHTML; | 68 .firstChild.innerHTML; |
69 }, | 69 }, |
70 | 70 |
71 /** | 71 /** |
72 * Similar to 'i18n', with an unused |locale| parameter used to trigger | 72 * Similar to 'i18n', with an unused |locale| parameter used to trigger |
73 * updates when |this.locale| changes. | 73 * updates when |this.locale| changes. |
74 * @param {string} locale The UI language used. | 74 * @param {string} locale The UI language used. |
75 * @param {string} id The ID of the string to translate. | 75 * @param {string} id The ID of the string to translate. |
76 * @param {...string} var_args Values to replace the placeholders $1 to $9 | 76 * @param {...string} var_args Values to replace the placeholders $1 to $9 |
77 * in the string. | 77 * in the string. |
78 * @return {string} A translated, sanitized, substituted string. | 78 * @return {string} A translated, sanitized, substituted string. |
79 */ | 79 */ |
80 i18nDynamic: function(locale, id, var_args) { | 80 i18nDynamic: function(locale, id, var_args) { |
81 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1)); | 81 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1)); |
82 }, | 82 }, |
83 | 83 |
84 /** | 84 /** |
85 * Returns true if a translation exists for |id|. | 85 * Returns true if a translation exists for |id|. |
86 * @param {string} id | 86 * @param {string} id |
87 * @return {boolean} | 87 * @return {boolean} |
88 */ | 88 */ |
89 i18nExists: function(id) { | 89 i18nExists: function(id) { |
90 return loadTimeData.valueExists(id); | 90 return loadTimeData.valueExists(id); |
91 }, | 91 }, |
92 | 92 |
93 /** | 93 /** |
94 * Call this when UI strings may have changed. This will send an update to any | 94 * Call this when UI strings may have changed. This will send an update to |
95 * data bindings to i18nDynamic(locale, ...). | 95 * any data bindings to i18nDynamic(locale, ...). |
96 */ | 96 */ |
97 i18nUpdateLocale: function() { | 97 i18nUpdateLocale: function() { |
98 this.locale = loadTimeData.getString('language'); | 98 this.locale = loadTimeData.getString('language'); |
99 }, | 99 }, |
100 }; | 100 }; |
101 | 101 |
102 /** | 102 /** |
103 * @typedef {{ | |
104 * substitutions: (Array<string>|undefined), | |
105 * attrs: (Object<function(Node, string):boolean>|undefined), | |
106 * tags: (Array<string>|undefined), | |
107 * }} | |
108 */ | |
109 var I18nAdvancedOpts; | |
110 | |
111 /** | |
103 * TODO(stevenjb): Replace with an interface. b/24294625 | 112 * TODO(stevenjb): Replace with an interface. b/24294625 |
104 * @typedef {{ | 113 * @typedef {{ |
105 * i18n: function(string, ...string): string}}, | 114 * i18n: function(string, ...string): string, |
106 * i18nAdvanced: function({ | 115 * i18nAdvanced: function(string, I18nAdvancedOpts=): string, |
107 * substitutions: (Array<string>|undefined), | 116 * i18nDynamic: function(string, string, ...string): string, |
108 * attrs: (Object<function(Node, string):boolean>|undefined), | 117 * i18nExists: function(string), |
109 * tags: (Array<string>|undefined)}, opts), | 118 * i18nUpdateLocale: function() |
110 * i18nExists: function(string) | |
111 * }} | 119 * }} |
112 */ | 120 */ |
113 I18nBehavior.Proto; | 121 I18nBehavior.Proto; |
OLD | NEW |