10.7 BabelFish Web Service

The BabelFish translation service at XMethods (http://www.xmethods.net) is a sample that can be implemented easily with a couple of text fields, a combo box, and a button. With a few lines of ActionScript code, you can put language translation functionality into a Flash movie and into your site. The BabelFish method at http://www.xmethods.net/sd/2001/BabelFishService.wsdl takes two arguments: translationmode and sourcedata.

The translationmode argument is a special code for the two languages used in the translation, as shown in Table 10-2. This data is displayed in a combo box named language_cb.

Table 10-2. Translation modes for the BabelFish remote method

From

To

translationmode

English

French

en_fr

English

German

en_de

English

Italian

en_it

English

Portuguese

en_pt

English

Spanish

en_es

French

English

fr_en

German

English

de_en

Italian

English

it_en

Portuguese

English

pt_en

Russian

English

ru_en

Spanish

English

es_en

The sourcedata argument is the text that you want translated (limited to a string of 150 characters or less). The Flash UI, shown in Figure 10-2, allows the user to type into the from_txt text field, choose a language translation from the combo box, click a button, and get the result in a text field named to_txt.

Figure 10-2. Translation interface
figs/frdg_1002.gif

The client-side ActionScript code is shown in Example 10-4, using the ColdFusion gateway.

Example 10-4. ActionScript code for consuming the BabelFish web service
#include "NetServices.as"

// Set up variables for the URL and service paths
// Use your own Flash Remoting gateway URL
var myURL = "http://localhost:8500/flashservices/gateway";
var servicePath = "http://www.xmethods.net/sd/2001/BabelFishService.wsdl";

// Define the custom class SimpleResult
function SimpleResult ( ) {
}
// Set up onResult( ) and onStatus( ) handlers as methods of SimpleResult class
SimpleResult.prototype.onResult = function (myResults) {
  to_txt.text = myResults;
};
SimpleResult.prototype.onStatus = function (myError) {
  to_txt.text = myError.description;
};
// Set the system status to be handled by the result status handler as well
System.onStatus = SimpleResult.protype.onStatus;

// Connection hasn't been initialized; create connection and service objects
if (initialized == null) {
  initialized = true;
  NetServices.setDefaultGatewayURL(myURL);
  var myConnection_conn = NetServices.createGatewayConnection( );
  var myService = myConnection_conn.getService(servicePath, new SimpleResult( ));
}

// Call the service on click of the push button
translate_pb.setClickHandler("translate");
function translate ( ) {
  myService.BabelFish(language_cb.getSelectedItem( ).data, from_txt.text);
}

The ActionScript code when using an ASP.NET server would differ only in the Flash Remoting gateway URL, as set in the myURL variable. Everything else remains the same.

The ActionScript code when using a PHP example is also similar. Once the gateway URL is set correctly, the only other difference is that the translate( ) function must pass the parameters to the BabelFish service using an object with named properties:

function translate ( ) {
  myService.BabelFish(
    {translationmode:language_cb.getSelectedItem( ).data,
     sourcedata:from_txt.text});
}

The Java version requires the wrapper JavaBean, similar to Example 10-3. The wrapper JavaBean needed for Example 10-4 is shown in Example 10-5. You will also have to use the wsdl2java tool as described earlier under Section 10.5.

Example 10-5. JavaBean wrapper for the BabelFish method
// Use the same package as the wsdl2java-generated classes
package net.xmethods.www;

public class BabelFishServiceBean implements java.io.Serializable {

// Handle to the generated stub
private net.xmethods.www.BabelFishBindingStub soap;

  // Empty constuctor
  public BabelFishServiceBean( ) throws java.net.MalformedURLException,
    org.apache.axis.AxisFault {
    final java.net.URL endPoint = new java.net.URL
    ("http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
     soap = new net.xmethods.www.BabelFishBindingStub(endPoint,
       new org.apache.axis.client.Service( ));
  }

  // Public method to call the web services method babelFish
  public java.lang.String babelFish(java.lang.String translationmode,
   java.lang.String sourcedata) throws java.rmi.RemoteException {
     return soap.babelFish(translationmode, sourcedata);
  }
}

The ActionScript code for the Java example needs to have the URL to the service changed from the .wsdl file to the JavaBean on your server:

var servicePath = "net.xmethods.www.BabelFishServiceBean";


    Part III: Advanced Flash Remoting