How to Translate Text to desired language ?

By | August 10, 2011

Just follow these steps and copy & paste the code you can translate text easily.

Step 1 : Make a div with id (id is compulsory)
Like

<div id="content">Loading...</div>

Step 2 : Paste the following code to head section under script tag of your webpage.

 

<script type="text/javascript">
google.load("language", "1");
 
function initialize() {
var content = document.getElementById('content');
// Setting the text in the div.
content.innerHTML = '&amp;lt;div id="text"&amp;gt;Place Your Text Here (Hola, me alegro mucho de verte.)&amp;lt;/div&amp;gt;&amp;lt;div id="translation"/&amp;gt;';
 
// Grabbing the text to translate
var text = document.getElementById("text").innerHTML;
 
// Translate from Spanish to English, and have the callback of the request
// put the resulting translation in the "translation" div.
// Note: by putting in an empty string for the source language ('es') then the translation
// will auto-detect the source language.
google.language.translate(text, 'es', 'en', function(result) {
var translated = document.getElementById("translation");
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
google.setOnLoadCallback(initialize);
</script>

Step 3 : And the final Step is paste the following URL in src of script tag.

<script type="text/javascript" src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0"&gt;&lt;/script>

Above example is translating Spanish to English You can translate as your choice.
Just change the language code in

google.language.translate(text, 'es', 'en', function(result)

es stands for Spanish,
en stands for English
You can find more language code at
http://code.google.com/apis/language/translate/v1/reference.html

Leave a Reply

Your email address will not be published. Required fields are marked *