Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This code is used in conjunction with the Google Translate Element script. | 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. | 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. | 7 // It should be included in the page before the Translate Element script. |
| 8 | 8 |
| 9 var cr = {}; | 9 var cr = {}; |
| 10 | 10 |
| 11 cr.googleTranslate = (function() { | 11 cr.googleTranslate = (function() { |
| 12 // Internal states. | 12 // Internal states. |
| 13 var lib; | 13 var lib; |
| 14 var libReady = false; | 14 var libReady = false; |
| 15 var error = false; | 15 var error = false; |
| 16 var finished = false; | 16 var finished = false; |
| 17 var checkReadyCount = 0; | 17 var checkReadyCount = 0; |
| 18 | 18 |
| 19 function checkLibReady() { | 19 function checkLibReady() { |
| 20 if (lib.isAvailable()) { | 20 if (lib.isAvailable()) { |
| 21 libReady = true; | 21 libReady = true; |
| 22 return; | 22 return; |
| 23 } | 23 } |
| 24 if (checkReadyCount++ > 5) { | 24 if (checkReadyCount++ > 5) { |
| 25 error = true; | 25 error = true; |
| 26 return; | 26 return; |
| 27 } | 27 } |
| 28 setTimeout(checkLibReady, 100); | 28 setTimeout(checkLibReady, 100); |
|
Dan Beam
2012/04/05 22:24:13
not sure if gross or slick...
| |
| 29 } | 29 } |
| 30 | 30 |
|
Dan Beam
2012/04/05 22:24:13
it's possible we'd want to doc this as well... mai
| |
| 31 function onTranslateProgress(progress, opt_finished, opt_error) { | 31 function onTranslateProgress(progress, opt_finished, opt_error) { |
| 32 finished = opt_finished; | 32 finished = opt_finished; |
| 33 // opt_error can be 'undefined'. | 33 // opt_error can be 'undefined'. |
| 34 if (typeof opt_error == 'boolean' && opt_error) { | 34 if (typeof opt_error == 'boolean' && opt_error) { |
| 35 error = true; | 35 error = true; |
| 36 // We failed to translate, restore so the page is in a consistent state. | 36 // We failed to translate, restore so the page is in a consistent state. |
| 37 lib.restore(); | 37 lib.restore(); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 63 * @type {boolean} | 63 * @type {boolean} |
| 64 */ | 64 */ |
| 65 get error() { | 65 get error() { |
| 66 return error; | 66 return error; |
| 67 }, | 67 }, |
| 68 | 68 |
| 69 /** | 69 /** |
| 70 * The language the page translated was in. Is valid only after the page | 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 | 71 * has been successfully translated and the original language specified to |
| 72 * the translate function was 'auto'. Is empty otherwise. | 72 * the translate function was 'auto'. Is empty otherwise. |
| 73 * @type {boolean} | 73 * @type {boolean} |
|
Dan Beam
2012/04/05 22:24:13
wrong @type, I smell copy pasta
| |
| 74 */ | 74 */ |
| 75 get sourceLang() { | 75 get sourceLang() { |
| 76 if (!libReady || !finished || error) | 76 if (!libReady || !finished || error) |
| 77 return ""; | 77 return ''; |
| 78 return lib.getDetectedLanguage(); | 78 return lib.getDetectedLanguage(); |
| 79 }, | 79 }, |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Translate the page contents. Note that the translation is asynchronous. | 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 | 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. | 84 * if the translation finished or if there was an error. |
| 85 * @param {string} originalLang The language the page is in. | 85 * @param {string} originalLang The language the page is in. |
| 86 * @param {string} targetLang The language the page should be translated to. | 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 | 87 * @return {boolean} False if the translate library was not ready, in which |
| 88 * case the translation is not started. True otherwise. | 88 * case the translation is not started. True otherwise. |
|
Dan Beam
2012/04/05 22:24:13
wrong indent, should be 4\s
| |
| 89 */ | 89 */ |
| 90 translate: function(originalLang, targetLang) { | 90 translate: function(originalLang, targetLang) { |
| 91 finished = false; | 91 finished = false; |
| 92 error = false; | 92 error = false; |
| 93 if (!libReady) | 93 if (!libReady) |
| 94 return false; | 94 return false; |
| 95 lib.translatePage(originalLang, targetLang, onTranslateProgress); | 95 lib.translatePage(originalLang, targetLang, onTranslateProgress); |
| 96 return true; | 96 return true; |
| 97 }, | 97 }, |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * Reverts the page contents to its original value, effectively reverting | 100 * Reverts the page contents to its original value, effectively reverting |
| 101 * any performed translation. Does nothing if the page was not translated. | 101 * any performed translation. Does nothing if the page was not translated. |
| 102 */ | 102 */ |
| 103 revert: function() { | 103 revert: function() { |
| 104 lib.restore(); | 104 lib.restore(); |
| 105 }, | 105 }, |
| 106 | 106 |
| 107 /** | 107 /** |
| 108 * Entry point called by the Translate Element once it has been injected in | 108 * Entry point called by the Translate Element once it has been injected in |
| 109 * the page. | 109 * the page. |
| 110 */ | 110 */ |
| 111 onTranslateElementLoad : function() { | 111 onTranslateElementLoad: function() { |
| 112 try { | 112 try { |
| 113 lib = google.translate.TranslateService({}); | 113 lib = google.translate.TranslateService({}); |
| 114 } catch(err) { | 114 } catch (err) { |
| 115 error = true; | 115 error = true; |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 // The TranslateService is not available immediately as it needs to start | 118 // The TranslateService is not available immediately as it needs to start |
| 119 // Flash. Let's wait until it is ready. | 119 // Flash. Let's wait until it is ready. |
| 120 checkLibReady(); | 120 checkLibReady(); |
| 121 } | 121 } |
| 122 }; | 122 }; |
| 123 })(); | 123 })(); |
| OLD | NEW |