Published 2009-10-14 07:01:25

If you have ever used googles translate API, it can quickly become a love hate relationship. You love the features it provides, but you begin to hate that fact that google's server are slow and flaky for loading the libraries that they recommend.

This situation was getting especially annoying yesterday, as the load time of my application (that is getting continually re-loaded while I'm working on it) was getting worse and worse, and google's API's was the culprit.

So after some further reading on that page, I realized that the translation call was really just a simple HTTP request with the correct parameters.. no need for huge google framework API etc.

So here's the ~20 line javascript to replace the slow loading 50k+ library that google recommends....

/**
* usage
* gtranslate('hello', 'en', 'es', function (res) {
* if (typeof(res) == 'object') { return; } // failure
* console.log(res); // success...
* });

*/

function gtranslate(str, src, dest, cb)
{
var x = new Roo.data.ScriptTagProxy({
url: 'http://ajax.googleapis.com/ajax/services/language/translate',
callbackParam : 'callback'
});
x.load(
{
v: '1.0',
q : str,
langpair : src + '|' +dest,
}, // end params.
{ // reader
readRecords : function (o) {
if (!o.responseData) {
return o;
}
return o.responseData.translatedText;
}
},
function (result) {
cb(result);
},
this,
[]
);


}



Mentioned By:
regator.com : Speedier google translate API for RooJs and ExtJS - Alan Knowles | related posts and comments (79 referals)
www.devcha.com : How to overcome Google Translate's Get Request Limits (414 Request-URI Too Large) using jQuery (49 referals)
google.com : google translate api (34 referals)
google.com : extjs api (28 referals)
google.com : google translate api php (21 referals)
google.com : google translate (18 referals)
www.planet-php.net : Planet PHP (14 referals)
google.com : extjs google translate (11 referals)
google.com : extjs translate (9 referals)
google.com : slow translate.googleapis.com (6 referals)
www.devcha.com : Challenges that each developer faces every day (5 referals)
google.com : google translate extjs (5 referals)
google.com : translate api (5 referals)
google.com : extjs google (4 referals)
google.com : translate (4 referals)
google.com : ext-js api (3 referals)
google.com : google translate3 (3 referals)
google.com : http://www.akbkhome.com/blog.php/View/182/Speedier_google_translate_API_for_RooJs_and_ExtJS.html (3 referals)
google.com : translate api php (3 referals)
regator.com : Alan Knowles | Regator (2 referals)

Add Your Comment

Follow us on