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