Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 This file defines a singleton which provides access to all data | 6 * @fileoverview This file defines a singleton which provides access to all data |
| 7 * that is available as soon as the page's resources are loaded (before DOM | 7 * that is available as soon as the page's resources are loaded (before DOM |
| 8 * content has finished loading). This data includes both localized strings and | 8 * content has finished loading). This data includes both localized strings and |
| 9 * any data that is important to have ready from a very early stage (e.g. things | 9 * any data that is important to have ready from a very early stage (e.g. things |
| 10 * that must be displayed right away). | 10 * that must be displayed right away). |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 * @param {string} id The ID of the string we want. | 80 * @param {string} id The ID of the string we want. |
| 81 * @param {...(string|number)} var_args The extra values to include in the | 81 * @param {...(string|number)} var_args The extra values to include in the |
| 82 * formatted output. | 82 * formatted output. |
| 83 * @return {string} The formatted string. | 83 * @return {string} The formatted string. |
| 84 */ | 84 */ |
| 85 getStringF: function(id, var_args) { | 85 getStringF: function(id, var_args) { |
| 86 var value = this.getString(id); | 86 var value = this.getString(id); |
| 87 if (!value) | 87 if (!value) |
| 88 return ''; | 88 return ''; |
| 89 | 89 |
| 90 var args = Array.prototype.slice.call(arguments); | |
| 91 args[0] = value; | |
| 92 return this.substituteString.apply(this, args); | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * Returns a formatted localized string where $1 to $9 are replaced by the | |
| 97 * second to the tenth argument. Any standalone $ signs must be escaped as | |
| 98 * $$. | |
| 99 * @param {string} label The label to substitute through. | |
| 100 * This is not an resource ID. | |
| 101 * @param {...(string|number)} var_args The extra values to include in the | |
| 102 * formatted output. | |
| 103 * @return {string} The formatted string. | |
| 104 */ | |
| 105 substituteString: function(label, var_args) { | |
| 90 var varArgs = arguments; | 106 var varArgs = arguments; |
| 91 return value.replace(/\$[$1-9]/g, function(m) { | 107 return label.replace(/\$(.|$)/g, function(m) { |
|
hirono
2017/06/09 01:02:21
/\$(.|$)/g does not match something like "$\nHi!".
calamity
2017/06/09 05:55:23
I'm not aware of any multiline strings.
Either wa
| |
| 108 assert(m.match(/\$[$1-9]/), 'Unescaped $ found in localized string.'); | |
| 92 return m == '$$' ? '$' : varArgs[m[1]]; | 109 return m == '$$' ? '$' : varArgs[m[1]]; |
| 93 }); | 110 }); |
| 94 }, | 111 }, |
| 95 | 112 |
| 96 /** | 113 /** |
| 114 * Returns a formatted string where $1 to $9 are replaced by the second to | |
| 115 * tenth argument, split apart into a list of pieces describing how the | |
| 116 * substitution was performed. Any standalone $ signs must be escaped as $$. | |
| 117 * @param {string} label A localized string to substitute through. | |
| 118 * This is not an resource ID. | |
| 119 * @param {...(string|number)} var_args The extra values to include in the | |
| 120 * formatted output. | |
| 121 * @return {!Array<!{value: string, arg: (null|string)}>} The formatted | |
| 122 * string pieces. | |
| 123 */ | |
| 124 getSubstitutedStringPieces: function(label, var_args) { | |
| 125 var varArgs = arguments; | |
| 126 // Split the string by separately matching all occurrences of $1-9 and of | |
| 127 // non $1-9 pieces. | |
| 128 var pieces = (label.match(/(\$[1-9])|(([^$]|\$([^1-9]|$))+)/g) || | |
|
hirono
2017/06/09 01:02:21
nit & optional: (label ? label.split(/(\$[1-9])/)
calamity
2017/06/09 05:55:23
This won't work for $$60 which I'd expect to rende
hirono
2017/06/09 06:01:25
That's true!
| |
| 129 []).map(function(p) { | |
| 130 // Pieces that are not $1-9 should be returned after replacing $$ | |
| 131 // with $. | |
| 132 if (!p.match(/^\$[1-9]$/)) { | |
| 133 assert( | |
| 134 (p.match(/\$/g) || []).length % 2 == 0, | |
| 135 'Unescaped $ found in localized string.'); | |
| 136 return {value: p.replace(/\$\$/g, '$'), arg: null}; | |
| 137 } | |
| 138 | |
| 139 // Otherwise, return the substitution value. | |
| 140 return {value: varArgs[p[1]], arg: p}; | |
| 141 }); | |
| 142 | |
| 143 return pieces; | |
| 144 }, | |
| 145 | |
| 146 /** | |
| 97 * As above, but also makes sure that the value is a boolean. | 147 * As above, but also makes sure that the value is a boolean. |
| 98 * @param {string} id The key that identifies the desired boolean. | 148 * @param {string} id The key that identifies the desired boolean. |
| 99 * @return {boolean} The corresponding boolean value. | 149 * @return {boolean} The corresponding boolean value. |
| 100 */ | 150 */ |
| 101 getBoolean: function(id) { | 151 getBoolean: function(id) { |
| 102 var value = this.getValue(id); | 152 var value = this.getValue(id); |
| 103 expectIsType(id, value, 'boolean'); | 153 expectIsType(id, value, 'boolean'); |
| 104 return /** @type {boolean} */ (value); | 154 return /** @type {boolean} */ (value); |
| 105 }, | 155 }, |
| 106 | 156 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 * @param {string} type The type we expect |value| to be. | 199 * @param {string} type The type we expect |value| to be. |
| 150 */ | 200 */ |
| 151 function expectIsType(id, value, type) { | 201 function expectIsType(id, value, type) { |
| 152 expect( | 202 expect( |
| 153 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); | 203 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); |
| 154 } | 204 } |
| 155 | 205 |
| 156 expect(!loadTimeData, 'should only include this file once'); | 206 expect(!loadTimeData, 'should only include this file once'); |
| 157 loadTimeData = new LoadTimeData; | 207 loadTimeData = new LoadTimeData; |
| 158 })(); | 208 })(); |
| OLD | NEW |