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.
9 * Localize an element by setting its innerText according to the specified tag
10 * and an optional set of substitutions.
11 * @param {Element} element The element to localize.
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 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
16 * This parameter should be used with caution.
17 * @return {boolean} True if the localization was successful; false otherwise.
19 l10n
.localizeElementFromTag = function(element
, tag
, opt_substitutions
,
21 var translation
= chrome
.i18n
.getMessage(tag
, opt_substitutions
);
23 console
.error('Missing translation for "' + tag
+ '":', element
);
24 translation
= tag
; // Make errors more obvious
27 element
.innerHTML
= translation
;
29 element
.innerText
= translation
;
31 return translation
!= null;
35 * Localize an element by setting its innerText according to its i18n-content
36 * attribute, and an optional set of substitutions.
37 * @param {Element} element The element to localize.
38 * @param {(string|Array)=} opt_substitutions An optional set of substitution
39 * strings corresponding to the "placeholders" attributes in messages.json.
40 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
41 * This parameter should be used with caution.
42 * @return {boolean} True if the localization was successful; false otherwise.
44 l10n
.localizeElement = function(element
, opt_substitutions
, opt_asHtml
) {
45 var tag
= element
.getAttribute('i18n-content');
46 return l10n
.localizeElementFromTag(element
, tag
, opt_substitutions
,
51 * Localize all tags with the i18n-content attribute, using i18n-data-n
52 * attributes to specify any placeholder substitutions.
54 * Because we use i18n-value attributes to implement translations of rich
55 * content (including paragraphs with hyperlinks), we localize these as
56 * HTML iff there are any substitutions.
58 l10n
.localize = function() {
59 var elements
= document
.querySelectorAll('[i18n-content]');
60 for (var i
= 0; i
< elements
.length
; ++i
) {
61 /** @type {Element} */ var element
= elements
[i
];
62 var substitutions
= [];
63 for (var j
= 1; j
< 9; ++j
) {
64 var value
= 'i18n-value-' + j
;
65 var valueName
= 'i18n-value-name-' + j
;
66 if (element
.hasAttribute(value
)) {
67 substitutions
.push(element
.getAttribute(value
));
68 } else if (element
.hasAttribute(valueName
)) {
69 var name
= element
.getAttribute(valueName
);
70 var translation
= chrome
.i18n
.getMessage(name
);
72 substitutions
.push(translation
);
74 console
.error('Missing translation for substitution: ' + name
);
75 substitutions
.push(name
);
81 l10n
.localizeElement(element
, substitutions
, substitutions
.length
!= 0);
83 // TODO(jamiewalch): Move this logic to the html document.
84 var editButton
= document
.getElementById('this-host-rename');
85 editButton
.title
= chrome
.i18n
.getMessage(/*i18n-content*/'TOOLTIP_RENAME');