| OLD | NEW |
| (Empty) |
| 1 /* Copyright (c) 2011 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 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 * @return {boolean} True if the localization was successful; false otherwise. | |
| 16 */ | |
| 17 l10n.localizeElementFromTag = function(element, tag, opt_substitutions) { | |
| 18 var translation = chrome.i18n.getMessage(tag, opt_substitutions); | |
| 19 if (translation) { | |
| 20 element.innerHTML = translation; | |
| 21 } else { | |
| 22 console.error('Missing translation for "' + tag + '":', element); | |
| 23 } | |
| 24 return translation != null; | |
| 25 }; | |
| 26 | |
| 27 /** | |
| 28 * Localize an element by setting its innerText according to its i18n-content | |
| 29 * attribute, and an optional set of substitutions. | |
| 30 * @param {Element} element The element to localize. | |
| 31 * @param {(string|Array)=} opt_substitutions An optional set of substitution | |
| 32 * strings corresponding to the "placeholders" attributes in messages.json. | |
| 33 * @return {boolean} True if the localization was successful; false otherwise. | |
| 34 */ | |
| 35 l10n.localizeElement = function(element, opt_substitutions) { | |
| 36 var tag = element.getAttribute('i18n-content'); | |
| 37 return l10n.localizeElementFromTag(element, tag, opt_substitutions); | |
| 38 }; | |
| 39 | |
| 40 /** | |
| 41 * Localize all tags with the i18n-content attribute, using i18n-data-n | |
| 42 * attributes to specify any placeholder substitutions. | |
| 43 */ | |
| 44 l10n.localize = function() { | |
| 45 var elements = document.querySelectorAll('[i18n-content]'); | |
| 46 for (var i = 0; i < elements.length; ++i) { | |
| 47 /** @type {Element} */ var element = elements[i]; | |
| 48 var substitutions = null; | |
| 49 for (var j = 1; j < 9; ++j) { | |
| 50 var attr = 'i18n-value-' + j; | |
| 51 if (element.hasAttribute(attr)) { | |
| 52 if (!substitutions) { | |
| 53 substitutions = []; | |
| 54 } | |
| 55 substitutions.push(element.getAttribute(attr)); | |
| 56 } else { | |
| 57 break; | |
| 58 } | |
| 59 } | |
| 60 l10n.localizeElement(element, substitutions); | |
| 61 } | |
| 62 }; | |
| OLD | NEW |