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 var l10n = l10n || {}; | |
7 | |
8 /** | |
9 * Localize a tag, returning the tag itself and logging an error if no | |
10 * translation exists. | |
11 * | |
12 * @param {string} tag The localization tag. | |
13 * @param {(string|Array)=} opt_substitutions An optional set of substitution | |
14 * strings corresponding to the "placeholders" attributes in messages.json. | |
15 * @return {string} The translated tag. | |
16 */ | |
17 l10n.getTranslationOrError = function(tag, opt_substitutions) { | |
18 var translation = chrome.i18n.getMessage(tag, opt_substitutions); | |
19 if (translation) { | |
20 return translation; | |
21 } | |
22 console.error('Missing translation for "' + tag + '"'); | |
23 return tag; | |
24 }; | |
25 | |
26 /** | |
27 * Localize an element by setting its innerText according to the specified tag | |
28 * and an optional set of substitutions. | |
29 * | |
30 * @param {Element} element The element to localize. | |
31 * @param {string} tag The localization tag or | |
32 * an Error object containing the tag. | |
33 * @param {(string|Array)=} opt_substitutions An optional set of substitution | |
34 * strings corresponding to the "placeholders" attributes in messages.json. | |
35 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText. | |
36 * This parameter should be used with caution. | |
37 * @return {boolean} True if the localization was successful; false otherwise. | |
38 */ | |
39 l10n.localizeElementFromTag = function(element, tag, opt_substitutions, | |
40 opt_asHtml) { | |
41 var translation = l10n.getTranslationOrError(tag, opt_substitutions); | |
42 if (opt_asHtml) { | |
43 element.innerHTML = translation; | |
44 } else { | |
45 element.innerText = translation; | |
46 } | |
47 return translation != null; | |
48 }; | |
49 | |
50 /** | |
51 * Localize an element by setting its innerText according to its i18n-content | |
52 * attribute, and an optional set of substitutions. | |
53 * | |
54 * @param {Element} element The element to localize. | |
55 * @param {(string|Array)=} opt_substitutions An optional set of substitution | |
56 * strings corresponding to the "placeholders" attributes in messages.json. | |
57 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText. | |
58 * This parameter should be used with caution. | |
59 * @return {boolean} True if the localization was successful; false otherwise. | |
60 */ | |
61 l10n.localizeElement = function(element, opt_substitutions, opt_asHtml) { | |
62 var tag = element.getAttribute('i18n-content'); | |
63 return l10n.localizeElementFromTag(element, tag, opt_substitutions, | |
64 opt_asHtml); | |
65 }; | |
66 | |
67 /** | |
68 * Localize all tags with the i18n-content attribute, using i18n-data-n | |
69 * attributes to specify any placeholder substitutions. | |
70 * | |
71 * Because we use i18n-value attributes to implement translations of rich | |
72 * content (including paragraphs with hyperlinks), we localize these as | |
73 * HTML iff there are any substitutions. | |
74 */ | |
75 l10n.localize = function() { | |
76 var elements = document.querySelectorAll('[i18n-content],[i18n-title]'); | |
77 for (var i = 0; i < elements.length; ++i) { | |
78 /** @type {Element} */ var element = elements[i]; | |
79 var substitutions = []; | |
80 for (var j = 1; j < 9; ++j) { | |
81 var value = 'i18n-value-' + j; | |
82 var valueName = 'i18n-value-name-' + j; | |
83 if (element.hasAttribute(value)) { | |
84 substitutions.push(element.getAttribute(value)); | |
85 } else if (element.hasAttribute(valueName)) { | |
86 var name = element.getAttribute(valueName); | |
87 var translation = chrome.i18n.getMessage(name); | |
88 if (translation) { | |
89 substitutions.push(translation); | |
90 } else { | |
91 console.error('Missing translation for substitution: ' + name); | |
92 substitutions.push(name); | |
93 } | |
94 } else { | |
95 break; | |
96 } | |
97 } | |
98 var titleTag = element.getAttribute('i18n-title'); | |
99 if (titleTag) { | |
100 element.title = l10n.getTranslationOrError(titleTag, substitutions); | |
101 } else { | |
102 l10n.localizeElement(element, substitutions, | |
103 substitutions.length != 0); | |
104 } | |
105 } | |
106 }; | |
OLD | NEW |