| 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 * NOTE: The use of this file is deprecated. Use load_time_data.js instead. | 6 * NOTE: The use of this file is deprecated. Use load_time_data.js instead. |
| 7 * | 7 * |
| 8 * The local strings get injected into the page using a variable named | 8 * The local strings get injected into the page using a variable named |
| 9 * {@code templateData}. This class provides a simpler interface to access those | 9 * {@code templateData}. This class provides a simpler interface to access those |
| 10 * strings. | 10 * strings. |
| 11 * | 11 * |
| 12 * @param {Object} opt_templateData Optional object containing translated | 12 * @param {Object=} opt_templateData Object containing translated strings. If |
| 13 * strings. If this is not supplied during construction, it can be | 13 * this is not supplied during construction, it can be assigned to the |
| 14 * assigned to the templateData property after construction. If all else | 14 * templateData property after construction. If all else fails, the value |
| 15 * fails, the value of window.templateDate will be used. | 15 * of window.templateDate will be used. |
| 16 |
| 16 * @constructor | 17 * @constructor |
| 17 */ | 18 */ |
| 18 function LocalStrings(opt_templateData) { | 19 function LocalStrings(opt_templateData) { |
| 19 this.templateData = opt_templateData; | 20 this.templateData = opt_templateData; |
| 20 } | 21 } |
| 21 | 22 |
| 22 // Start of anonymous namespace. | 23 // Start of anonymous namespace. |
| 23 (function() { | 24 (function() { |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * Returns a formatted string where $1 to $9 are replaced by the second to the | 27 * Returns a formatted string where $1 to $9 are replaced by the second to the |
| 27 * tenth argument. | 28 * tenth argument. |
| 28 * @param {string} s The format string. | 29 * @param {string} s The format string. |
| 29 * @param {...string} The extra values to include in the formatted output. | 30 * @param {Arguments} args The extra values to include in the formatted |
| 31 * output. |
| 30 * @return {string} The string after format substitution. | 32 * @return {string} The string after format substitution. |
| 31 */ | 33 */ |
| 32 function replaceArgs(s, args) { | 34 function replaceArgs(s, args) { |
| 33 return s.replace(/\$[$1-9]/g, function(m) { | 35 return s.replace(/\$[$1-9]/g, function(m) { |
| 34 return (m == '$$') ? '$' : args[m[1]]; | 36 return (m == '$$') ? '$' : args[m[1]]; |
| 35 }); | 37 }); |
| 36 } | 38 } |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Returns a string after removing Windows-style accelerators. | 41 * Returns a string after removing Windows-style accelerators. |
| 40 * @param {string} s The input string that may contain accelerators. | 42 * @param {string} s The input string that may contain accelerators. |
| 41 * @return {string} The resulting string with accelerators removed. | 43 * @return {string} The resulting string with accelerators removed. |
| 42 */ | 44 */ |
| 43 function trimAccelerators(s) { | 45 function trimAccelerators(s) { |
| 44 return s.replace(/&{1,2}/g, function(m) { | 46 return s.replace(/&{1,2}/g, function(m) { |
| 45 return (m == '&&') ? '&' : ''; | 47 return (m == '&&') ? '&' : ''; |
| 46 }); | 48 }); |
| 47 } | 49 } |
| 48 | 50 |
| 49 LocalStrings.prototype = { | 51 LocalStrings.prototype = { |
| 50 /** | 52 /** |
| 51 * The template data object. | 53 * The template data object. |
| 52 * @type {Object} | 54 * @type {Object|undefined} |
| 53 */ | 55 */ |
| 54 templateData: null, | 56 templateData: undefined, |
| 55 | 57 |
| 56 /** | 58 /** |
| 57 * Gets a localized string by its id. | 59 * Gets a localized string by its id. |
| 58 * @param {string} s The ID of the string we want. | 60 * @param {string} id The ID of the string we want. |
| 59 * @return {string} The localized string. | 61 * @return {string} The localized string. |
| 60 */ | 62 */ |
| 61 getString: function(id) { | 63 getString: function(id) { |
| 62 // TODO(arv): We should not rely on a global variable here. | 64 // TODO(arv): We should not rely on a global variable here. |
| 63 var templateData = this.templateData || window.templateData; | 65 var templateData = this.templateData || window.templateData; |
| 64 var str = templateData[id]; | 66 var str = templateData[id]; |
| 65 // TODO(jhawkins): Change to console.error when all errors are fixed. | 67 // TODO(jhawkins): Change to console.error when all errors are fixed. |
| 66 if (!str) | 68 if (!str) |
| 67 console.warn('Missing string for id: ' + id); | 69 console.warn('Missing string for id: ' + id); |
| 68 return str; | 70 return str; |
| 69 }, | 71 }, |
| 70 | 72 |
| 71 /** | 73 /** |
| 72 * Returns a formatted localized string where $1 to $9 are replaced by the | 74 * Returns a formatted localized string where $1 to $9 are replaced by the |
| 73 * second to the tenth argument. | 75 * second to the tenth argument. |
| 74 * @param {string} id The ID of the string we want. | 76 * @param {string} id The ID of the string we want. |
| 75 * @param {...string} The extra values to include in the formatted output. | 77 * @param {...string} var_args The extra values to include in the formatted |
| 78 * output. |
| 76 * @return {string} The formatted string. | 79 * @return {string} The formatted string. |
| 77 */ | 80 */ |
| 78 getStringF: function(id, var_args) { | 81 getStringF: function(id, var_args) { |
| 79 return replaceArgs(this.getString(id), arguments); | 82 return replaceArgs(this.getString(id), arguments); |
| 80 }, | 83 }, |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 // End of anonymous namespace. | 86 // End of anonymous namespace. |
| 84 })(); | 87 })(); |
| OLD | NEW |