Chromium Code Reviews| Index: ui/webui/resources/js/load_time_data.js |
| diff --git a/ui/webui/resources/js/load_time_data.js b/ui/webui/resources/js/load_time_data.js |
| index 38b4721e13c9a4ac3d4c5da425ec84e38396b5e0..b2f3798f69bd4c4988dd0818e60094bccaf16eaf 100644 |
| --- a/ui/webui/resources/js/load_time_data.js |
| +++ b/ui/webui/resources/js/load_time_data.js |
| @@ -87,12 +87,62 @@ function LoadTimeData(){} |
| if (!value) |
| return ''; |
| + var args = Array.prototype.slice.call(arguments); |
| + args[0] = value; |
| + return this.substituteString.apply(this, args); |
| + }, |
| + |
| + /** |
| + * Returns a formatted localized string where $1 to $9 are replaced by the |
| + * second to the tenth argument. Any standalone $ signs must be escaped as |
| + * $$. |
| + * @param {string} label The label to substitute through. |
| + * This is not an resource ID. |
| + * @param {...(string|number)} var_args The extra values to include in the |
| + * formatted output. |
| + * @return {string} The formatted string. |
| + */ |
| + substituteString: function(label, var_args) { |
| var varArgs = arguments; |
| - return value.replace(/\$[$1-9]/g, function(m) { |
| + 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
|
| + assert(m.match(/\$[$1-9]/), 'Unescaped $ found in localized string.'); |
| return m == '$$' ? '$' : varArgs[m[1]]; |
| }); |
| }, |
| + /** |
| + * Returns a formatted string where $1 to $9 are replaced by the second to |
| + * tenth argument, split apart into a list of pieces describing how the |
| + * substitution was performed. Any standalone $ signs must be escaped as $$. |
| + * @param {string} label A localized string to substitute through. |
| + * This is not an resource ID. |
| + * @param {...(string|number)} var_args The extra values to include in the |
| + * formatted output. |
| + * @return {!Array<!{value: string, arg: (null|string)}>} The formatted |
| + * string pieces. |
| + */ |
| + getSubstitutedStringPieces: function(label, var_args) { |
| + var varArgs = arguments; |
| + // Split the string by separately matching all occurrences of $1-9 and of |
| + // non $1-9 pieces. |
| + 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!
|
| + []).map(function(p) { |
| + // Pieces that are not $1-9 should be returned after replacing $$ |
| + // with $. |
| + if (!p.match(/^\$[1-9]$/)) { |
| + assert( |
| + (p.match(/\$/g) || []).length % 2 == 0, |
| + 'Unescaped $ found in localized string.'); |
| + return {value: p.replace(/\$\$/g, '$'), arg: null}; |
| + } |
| + |
| + // Otherwise, return the substitution value. |
| + return {value: varArgs[p[1]], arg: p}; |
| + }); |
| + |
| + return pieces; |
| + }, |
| + |
| /** |
| * As above, but also makes sure that the value is a boolean. |
| * @param {string} id The key that identifies the desired boolean. |