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.
32 * @param {(string|Array)=} opt_substitutions An optional set of substitution
33 * strings corresponding to the "placeholders" attributes in messages.json.
34 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
35 * This parameter should be used with caution.
36 * @return {boolean} True if the localization was successful; false otherwise.
38 l10n.localizeElementFromTag = function(element, tag, opt_substitutions,
40 var translation = l10n.getTranslationOrError(tag, opt_substitutions);
42 element.innerHTML = translation;
44 element.innerText = translation;
46 return translation != null;
50 * Localize an element by setting its innerText according to its i18n-content
51 * attribute, and an optional set of substitutions.
53 * @param {Element} element The element to localize.
54 * @param {(string|Array)=} opt_substitutions An optional set of substitution
55 * strings corresponding to the "placeholders" attributes in messages.json.
56 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
57 * This parameter should be used with caution.
58 * @return {boolean} True if the localization was successful; false otherwise.
60 l10n.localizeElement = function(element, opt_substitutions, opt_asHtml) {
61 var tag = element.getAttribute('i18n-content');
62 return l10n.localizeElementFromTag(element, tag, opt_substitutions,
67 * Localize all tags with the i18n-content attribute, using i18n-data-n
68 * attributes to specify any placeholder substitutions.
70 * Because we use i18n-value attributes to implement translations of rich
71 * content (including paragraphs with hyperlinks), we localize these as
72 * HTML iff there are any substitutions.
74 l10n.localize = function() {
75 var elements = document.querySelectorAll('[i18n-content],[i18n-title]');
76 for (var i = 0; i < elements.length; ++i) {
77 /** @type {Element} */ var element = elements[i];
78 var substitutions = [];
79 for (var j = 1; j < 9; ++j) {
80 var value = 'i18n-value-' + j;
81 var valueName = 'i18n-value-name-' + j;
82 if (element.hasAttribute(value)) {
83 substitutions.push(element.getAttribute(value));
84 } else if (element.hasAttribute(valueName)) {
85 var name = element.getAttribute(valueName);
86 var translation = chrome.i18n.getMessage(name);
88 substitutions.push(translation);
90 console.error('Missing translation for substitution: ' + name);
91 substitutions.push(name);
97 var titleTag = element.getAttribute('i18n-title');
99 element.title = l10n.getTranslationOrError(titleTag, substitutions);
101 l10n.localizeElement(element, substitutions,
102 substitutions.length != 0);