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(language, id, var_args) { | |
78 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1)); | |
Dan Beam
2017/05/22 23:39:02
i think this slice here is fine
dschuyler
2017/05/23 00:03:19
Acknowledged.
| |
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 (e.g. because the locale | |
92 * changed). This will send an update to any data bindings to | |
93 * i18nDynamic(locale, ...). | |
94 */ | |
95 updateLocalizedContent: function() { | |
96 this.locale = loadTimeData.getString('language'); | |
97 }, | |
70 }; | 98 }; |
71 | 99 |
72 /** | 100 /** |
73 * TODO(stevenjb): Replace with an interface. b/24294625 | 101 * TODO(stevenjb): Replace with an interface. b/24294625 |
74 * @typedef {{ | 102 * @typedef {{ |
75 * i18n: function(string, ...string): string}}, | 103 * i18n: function(string, ...string): string}}, |
76 * i18nAdvanced: function({ | 104 * i18nAdvanced: function({ |
77 * substitutions: (Array<string>|undefined), | 105 * substitutions: (Array<string>|undefined), |
78 * attrs: (Object<function(Node, string):boolean>|undefined), | 106 * attrs: (Object<function(Node, string):boolean>|undefined), |
79 * tags: (Array<string>|undefined)}, opts), | 107 * tags: (Array<string>|undefined)}, opts), |
80 * i18nExists: function(string) | 108 * i18nExists: function(string) |
81 * }} | 109 * }} |
82 */ | 110 */ |
83 I18nBehavior.Proto; | 111 I18nBehavior.Proto; |
OLD | NEW |