Related
Speedier google translate API for RooJs and ExtJS
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....
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,
[]
);
}
Add a comment (requires javascript!)
Follow us
-
- Roo J Solutions Limited is recruiting
- Free your data... seed webkit browser mirror button
- Deleting the View and Controller..
- What was I doing last night... Seed querying xscreensaver
- Watch-out PHP 5.3.7+ is about.. and the is_a() / __autoload() mess.
- Cli parsing in FlexyFramework, PEAR Console_GetArg
- Gtk3 introspection updates and Unusable Unity..
- How to spam in PHP..
Blog Latest
-
Twitter - @Roojs

Comments