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.