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

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

Issue 2886843005: [i18n] use Polymer data binding to change locale strings (Closed)
Patch Set: using I18nBehavior Created 3 years, 7 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
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 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 /*
Dan Beam 2017/05/22 23:26:12 should be /**
dschuyler 2017/05/22 23:31:42 Done in prior patch (this was the error reported b
15 * The language the UI is presented in. Used to signal dynamic language
16 * change.
17 * @private
18 */
19 locale: {
20 type: String,
21 value: '',
22 },
23 },
24
18 /** 25 /**
19 * Returns a translated string where $1 to $9 are replaced by the given 26 * Returns a translated string where $1 to $9 are replaced by the given
20 * values. 27 * values.
21 * @param {string} id The ID of the string to translate. 28 * @param {string} id The ID of the string to translate.
22 * @param {...string} var_args Values to replace the placeholders $1 to $9 29 * @param {...string} var_args Values to replace the placeholders $1 to $9
23 * in the string. 30 * in the string.
24 * @return {string} A translated, substituted string. 31 * @return {string} A translated, substituted string.
25 * @private 32 * @private
26 */ 33 */
27 i18nRaw_: function(id, var_args) { 34 i18nRaw_: function(id, var_args) {
(...skipping 25 matching lines...) Expand all
53 * tags: (Array<string>|undefined)}} opts 60 * tags: (Array<string>|undefined)}} opts
54 */ 61 */
55 i18nAdvanced: function(id, opts) { 62 i18nAdvanced: function(id, opts) {
56 var args = [id].concat(opts.substitutions || []); 63 var args = [id].concat(opts.substitutions || []);
57 var rawString = this.i18nRaw_.apply(this, args); 64 var rawString = this.i18nRaw_.apply(this, args);
58 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs) 65 return parseHtmlSubset('<b>' + rawString + '</b>', opts.tags, opts.attrs)
59 .firstChild.innerHTML; 66 .firstChild.innerHTML;
60 }, 67 },
61 68
62 /** 69 /**
70 * Similar to 'i18n', with updates whenever |language| changes.
stevenjb 2017/05/22 23:07:37 nit: Similar to 'i18n', with an unused |locale| pa
dschuyler 2017/05/22 23:30:17 Done.
71 * @param {string} language The UI language used.
stevenjb 2017/05/22 23:07:37 locale ?
dschuyler 2017/05/22 23:30:17 Done.
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));
dschuyler 2017/05/22 23:30:17 Hmm, I've recently read that splicing |arguments|
stevenjb 2017/05/22 23:37:00 splicing or slicing? I wouldn't think that slice h
dschuyler 2017/05/23 00:03:19 Yep, slicing. Thanks for catching the typo.
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 * Called when UI strings change.
stevenjb 2017/05/22 23:07:37 nit: Call this when UI strings may have changed (e
dschuyler 2017/05/22 23:30:17 Changed to something similar, though not exact. d
92 */
93 updateLocalizedContent: function() {
Dan Beam 2017/05/22 23:15:56 why shouldn't callers just set this.locale directl
dschuyler 2017/05/22 23:30:17 They sure could. I was trying to separate I18nBeha
94 this.locale = loadTimeData.getString('language');
95 },
70 }; 96 };
71 97
72 /** 98 /**
73 * TODO(stevenjb): Replace with an interface. b/24294625 99 * TODO(stevenjb): Replace with an interface. b/24294625
74 * @typedef {{ 100 * @typedef {{
75 * i18n: function(string, ...string): string}}, 101 * i18n: function(string, ...string): string}},
76 * i18nAdvanced: function({ 102 * i18nAdvanced: function({
77 * substitutions: (Array<string>|undefined), 103 * substitutions: (Array<string>|undefined),
78 * attrs: (Object<function(Node, string):boolean>|undefined), 104 * attrs: (Object<function(Node, string):boolean>|undefined),
79 * tags: (Array<string>|undefined)}, opts), 105 * tags: (Array<string>|undefined)}, opts),
80 * i18nExists: function(string) 106 * i18nExists: function(string)
81 * }} 107 * }}
82 */ 108 */
83 I18nBehavior.Proto; 109 I18nBehavior.Proto;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698