Home | History | Annotate | Download | only in webapp
      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.
     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.
     37  */
     38 l10n.localizeElementFromTag = function(element, tag, opt_substitutions,
     39                                        opt_asHtml) {
     40   var translation = l10n.getTranslationOrError(tag, opt_substitutions);
     41   if (opt_asHtml) {
     42     element.innerHTML = translation;
     43   } else {
     44     element.innerText = translation;
     45   }
     46   return translation != null;
     47 };
     48 
     49 /**
     50  * Localize an element by setting its innerText according to its i18n-content
     51  * attribute, and an optional set of substitutions.
     52  *
     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.
     59  */
     60 l10n.localizeElement = function(element, opt_substitutions, opt_asHtml) {
     61   var tag = element.getAttribute('i18n-content');
     62   return l10n.localizeElementFromTag(element, tag, opt_substitutions,
     63                                      opt_asHtml);
     64 };
     65 
     66 /**
     67  * Localize all tags with the i18n-content attribute, using i18n-data-n
     68  * attributes to specify any placeholder substitutions.
     69  *
     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.
     73  */
     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);
     87         if (translation) {
     88           substitutions.push(translation);
     89         } else {
     90           console.error('Missing translation for substitution: ' + name);
     91           substitutions.push(name);
     92         }
     93       } else {
     94         break;
     95       }
     96     }
     97     var titleTag = element.getAttribute('i18n-title');
     98     if (titleTag) {
     99       element.title = l10n.getTranslationOrError(titleTag, substitutions);
    100     } else {
    101       l10n.localizeElement(element, substitutions,
    102                            substitutions.length != 0);
    103     }
    104   }
    105 };
    106