Google just announced their new Language API this morning. Unfortunately their API is another one of their AJAX APIs – that are designed to be used from JavaScript in web pages. These APIs are pretty cool for building client-side web applications – I used their AJAX Feeds API in my home page – but I had some ideas for server software that could use a translation API.
I remembered John Resig’s hack from a few months back, getting enough of the DOM in Rhino to run some of the jQuery unit tests. I pulled that down, wrote the bits of DOM that were missing and now I’ve got a Java and JavaScript environment for accessing Google’s AJAX APIs. Apart creating stubs for some methods that are called the main functionality I had to implement was turning Google’s APIs’ asynchronous script loading into the Rhino shell’s load(url) calls. They use <script src="... and document.createElement("script"), but both are pretty easy to catch. The upshot of this is that everything is synchronous. This subtly changes a lot of the interface. For example, my Language API example looks like this:
load('ajax-environment.js');
load('http://www.google.com/jsapi');
google.load('language', '1');
google.language.translate("Hello world", "en", "es",
function(result) {
print(result.translation);
});
it of course prints: Hola mundo.
I’ve put the source up on github. Have a play, tell me what you think.


One Comment
We’ve implemented this in a live web to jabber chat client called Hab.la (which works within livejournal). Feel free to check it out at our blog Basically you can chat with users to your website in any language, seamlessly, and for free.
2 Trackbacks
[...] Software and Opinions » Blog Archive » Google AJAX APIs outside the browser Running a Javascript API outside the browser. Another example of how AJAX APIs are also web service APIs. (tags: ajax webservices) [...]
[...] played around with Google’s new AJAX Translation API before and I wondered how hard it would be to use that from a GreaseMonkey script. The answer: hard. [...]