Home | History | Annotate | Download | only in resources
      1 // Copyright (c) 2010 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 // This code is used in conjunction with the Google Translate Element script.
      6 // It is injected in a page to translate it from one language to another.
      7 // It should be included in the page before the Translate Element script.
      8 
      9 var cr = {};
     10 
     11 cr.googleTranslate = (function() {
     12   // Internal states.
     13   var lib;
     14   var libReady = false;
     15   var error = false;
     16   var finished = false;
     17   var checkReadyCount = 0;
     18 
     19   function checkLibReady() {
     20     if (lib.isAvailable()) {
     21       libReady = true;
     22       return;
     23     }
     24     if (checkReadyCount++ > 5) {
     25       error = true;
     26       return;
     27     }
     28     setTimeout(checkLibReady, 100);
     29   }
     30 
     31   function onTranslateProgress(progress, opt_finished, opt_error) {
     32     finished = opt_finished;
     33     // opt_error can be 'undefined'.
     34     if (typeof opt_error == 'boolean' && opt_error) {
     35       error = true;
     36       // We failed to translate, restore so the page is in a consistent state.
     37       lib.restore();
     38     }
     39   }
     40 
     41   // Public API.
     42   return {
     43     /**
     44      * Whether the library is ready.
     45      * The translate function should only be called when |libReady| is true.
     46      * @type {boolean}
     47      */
     48     get libReady() {
     49       return libReady;
     50     },
     51 
     52     /**
     53      * Whether the current translate has finished successfully.
     54      * @type {boolean}
     55      */
     56     get finished() {
     57       return finished;
     58     },
     59 
     60     /**
     61      * Whether an error occured initializing the library of translating the
     62      * page.
     63      * @type {boolean}
     64      */
     65     get error() {
     66       return error;
     67     },
     68 
     69     /**
     70      * The language the page translated was in. Is valid only after the page
     71      * has been successfully translated and the original language specified to
     72      * the translate function was 'auto'. Is empty otherwise.
     73      * @type {boolean}
     74      */
     75     get sourceLang() {
     76       if (!libReady || !finished || error)
     77         return "";
     78       return lib.getDetectedLanguage();
     79     },
     80 
     81     /**
     82      * Translate the page contents.  Note that the translation is asynchronous.
     83      * You need to regularly check the state of |finished| and |error| to know
     84      * if the translation finished or if there was an error.
     85      * @param {string} originalLang The language the page is in.
     86      * @param {string} targetLang The language the page should be translated to.
     87      * @return {boolean} False if the translate library was not ready, in which
     88      *                   case the translation is not started.  True otherwise.
     89      */
     90     translate: function(originalLang, targetLang) {
     91       finished = false;
     92       error = false;
     93       if (!libReady)
     94         return false;
     95       lib.translatePage(originalLang, targetLang, onTranslateProgress);
     96       return true;
     97     },
     98 
     99     /**
    100      * Reverts the page contents to its original value, effectively reverting
    101      * any performed translation.  Does nothing if the page was not translated.
    102      */
    103     revert: function() {
    104       lib.restore();
    105     },
    106 
    107     /**
    108      * Entry point called by the Translate Element once it has been injected in
    109      * the page.
    110      */
    111     onTranslateElementLoad : function() {
    112       try {
    113         lib = google.translate.TranslateService({});
    114       } catch(err) {
    115         error = true;
    116         return;
    117       }
    118       // The TranslateService is not available immediately as it needs to start
    119       // Flash.  Let's wait until it is ready.
    120       checkLibReady();
    121     }
    122   };
    123 })();
    124