| |
| 1 |
/* |
| 2 |
* How to translate text. |
| 3 |
*/ |
| 4 |
|
| 5 |
google.load("language", "1"); |
| 6 |
|
| 7 |
function initialize() { |
| 8 |
var content = document.getElementById('content'); |
| 9 |
// Setting the text in the div. |
| 10 |
content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="translation"/>'; |
| 11 |
|
| 12 |
// Grabbing the text to translate |
| 13 |
var text = document.getElementById("text").innerHTML; |
| 14 |
|
| 15 |
// Translate from Spanish to English, and have the callback of the request |
| 16 |
// put the resulting translation in the "translation" div. |
| 17 |
// Note: by putting in an empty string for the source language ('es') then the translation |
| 18 |
// will auto-detect the source language. |
| 19 |
google.language.translate(text, 'es', 'en', function(result) { |
| 20 |
var translated = document.getElementById("translation"); |
| 21 |
if (result.translation) { |
| 22 |
translated.innerHTML = result.translation; |
| 23 |
} |
| 24 |
}); |
| 25 |
} |
| 26 |
google.setOnLoadCallback(initialize); |
| |