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
3 // found in the LICENSE file.
6 * NOTE: The use of this file is deprecated. Use load_time_data.js instead.
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
12 * @param {Object} opt_templateData Optional object containing translated
13 * strings. If this is not supplied during construction, it can be
14 * assigned to the templateData property after construction. If all else
15 * fails, the value of window.templateDate will be used.
18 function LocalStrings(opt_templateData) {
19 this.templateData = opt_templateData;
22 // Start of anonymous namespace.
26 * Returns a formatted string where $1 to $9 are replaced by the second to the
28 * @param {string} s The format string.
29 * @param {...string} The extra values to include in the formatted output.
30 * @return {string} The string after format substitution.
32 function replaceArgs(s, args) {
33 return s.replace(/\$[$1-9]/g, function(m) {
34 return (m == '$$') ? '$' : args[m[1]];
39 * Returns a string after removing Windows-style accelerators.
40 * @param {string} s The input string that may contain accelerators.
41 * @return {string} The resulting string with accelerators removed.
43 function trimAccelerators(s) {
44 return s.replace(/&{1,2}/g, function(m) {
45 return (m == '&&') ? '&' : '';
49 LocalStrings.prototype = {
51 * The template data object.
57 * Gets a localized string by its id.
58 * @param {string} s The ID of the string we want.
59 * @return {string} The localized string.
61 getString: function(id) {
62 // TODO(arv): We should not rely on a global variable here.
63 var templateData = this.templateData || window.templateData;
64 var str = templateData[id];
65 // TODO(jhawkins): Change to console.error when all errors are fixed.
67 console.warn('Missing string for id: ' + id);
72 * Returns a formatted localized string where $1 to $9 are replaced by the
73 * second to the tenth argument.
74 * @param {string} id The ID of the string we want.
75 * @param {...string} The extra values to include in the formatted output.
76 * @return {string} The formatted string.
78 getStringF: function(id, var_args) {
79 return replaceArgs(this.getString(id), arguments);
83 // End of anonymous namespace.