Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: ui/webui/resources/js/i18n_behavior.js

Issue 2927213002: [MD settings] I18nBehavior return textContent from i18n() (Closed)
Patch Set: merge with master Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/test/data/webui/i18n_behavior_test.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(/&nbsp;/g, '\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 opts = opts || {};
65 var args = [id].concat(opts.substitutions || []); 64 var args = [id].concat(opts.substitutions || []);
66 var rawString = this.i18nRaw_.apply(this, args); 65 var rawString = this.i18nRaw_.apply(this, args);
67 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) 66 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs)
68 .firstChild.innerHTML; 67 .firstChild.innerHTML;
69 }, 68 },
70 69
71 /** 70 /**
72 * Similar to 'i18n', with an unused |locale| parameter used to trigger 71 * Similar to 'i18n', with an unused |locale| parameter used to trigger
73 * updates when |this.locale| changes. 72 * updates when |this.locale| changes.
74 * @param {string} locale The UI language used. 73 * @param {string} locale The UI language used.
75 * @param {string} id The ID of the string to translate. 74 * @param {string} id The ID of the string to translate.
76 * @param {...string} var_args Values to replace the placeholders $1 to $9 75 * @param {...string} var_args Values to replace the placeholders $1 to $9
77 * in the string. 76 * in the string.
78 * @return {string} A translated, sanitized, substituted string. 77 * @return {string} A translated, sanitized, substituted string.
79 */ 78 */
80 i18nDynamic: function(locale, id, var_args) { 79 i18nDynamic: function(locale, id, var_args) {
81 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1)); 80 return this.i18n.apply(this, Array.prototype.slice.call(arguments, 1));
82 }, 81 },
83 82
84 /** 83 /**
85 * Returns true if a translation exists for |id|. 84 * Returns true if a translation exists for |id|.
86 * @param {string} id 85 * @param {string} id
87 * @return {boolean} 86 * @return {boolean}
88 */ 87 */
89 i18nExists: function(id) { 88 i18nExists: function(id) {
90 return loadTimeData.valueExists(id); 89 return loadTimeData.valueExists(id);
91 }, 90 },
92 91
93 /** 92 /**
94 * Call this when UI strings may have changed. This will send an update to any 93 * Call this when UI strings may have changed. This will send an update to
95 * data bindings to i18nDynamic(locale, ...). 94 * any data bindings to i18nDynamic(locale, ...).
96 */ 95 */
97 i18nUpdateLocale: function() { 96 i18nUpdateLocale: function() {
98 this.locale = loadTimeData.getString('language'); 97 this.locale = loadTimeData.getString('language');
99 }, 98 },
100 }; 99 };
101 100
102 /** 101 /**
102 * @typedef {{
103 * substitutions: (Array<string>|undefined),
104 * attrs: (Object<function(Node, string):boolean>|undefined),
105 * tags: (Array<string>|undefined),
106 * }}
107 */
108 var I18nAdvancedOpts;
109
110 /**
103 * TODO(stevenjb): Replace with an interface. b/24294625 111 * TODO(stevenjb): Replace with an interface. b/24294625
104 * @typedef {{ 112 * @typedef {{
105 * i18n: function(string, ...string): string}}, 113 * i18n: function(string, ...string): string,
106 * i18nAdvanced: function({ 114 * i18nAdvanced: function(string, I18nAdvancedOpts=): string,
107 * substitutions: (Array<string>|undefined), 115 * i18nDynamic: function(string, string, ...string): string,
108 * attrs: (Object<function(Node, string):boolean>|undefined), 116 * i18nExists: function(string),
109 * tags: (Array<string>|undefined)}, opts), 117 * i18nUpdateLocale: function()
110 * i18nExists: function(string)
111 * }} 118 * }}
112 */ 119 */
113 I18nBehavior.Proto; 120 I18nBehavior.Proto;
OLDNEW
« no previous file with comments | « chrome/test/data/webui/i18n_behavior_test.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698