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 a tag, returning the tag itself and logging an error if no
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.
17 l10n
.getTranslationOrError = function(tag
, opt_substitutions
) {
18 var translation
= chrome
.i18n
.getMessage(tag
, opt_substitutions
);
22 console
.error('Missing translation for "' + tag
+ '"');
27 * Localize an element by setting its innerText according to the specified tag
28 * and an optional set of substitutions.
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.
39 l10n
.localizeElementFromTag = function(element
, tag
, opt_substitutions
,
41 var translation
= l10n
.getTranslationOrError(tag
, opt_substitutions
);
43 element
.innerHTML
= translation
;
45 element
.innerText
= translation
;
47 return translation
!= null;
51 * Localize an element by setting its innerText according to its i18n-content
52 * attribute, and an optional set of substitutions.
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.
61 l10n
.localizeElement = function(element
, opt_substitutions
, opt_asHtml
) {
62 var tag
= element
.getAttribute('i18n-content');
63 return l10n
.localizeElementFromTag(element
, tag
, opt_substitutions
,
68 * Localize all tags with the i18n-content attribute, using i18n-data-n
69 * attributes to specify any placeholder substitutions.
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.
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
);
89 substitutions
.push(translation
);
91 console
.error('Missing translation for substitution: ' + name
);
92 substitutions
.push(name
);
98 var titleTag
= element
.getAttribute('i18n-title');
100 element
.title
= l10n
.getTranslationOrError(titleTag
, substitutions
);
102 l10n
.localizeElement(element
, substitutions
,
103 substitutions
.length
!= 0);