The new WebServiceConnector component in Flash MX 2004 Professional makes talking to standard (SOAP) web services extremely easy. Here’s how to make a simple call to the BabelFish web service using Flash MX 2004 Professional’s new features:
http://www.xmethods.net/sd/2001/BabelFishService.wsdl
Now let’s create the interface of the application:
Let’s bind the babelfish component to the interface:
Now is a good time to save the FLA.
Before we can make this work, we need to populate the ComboBox. First, let’s take a look at how the web interface of BabelFish looks like: http://babelfish.altavista.com/. From the dropdown menus, you can tell the languages and translation directions.
View Source from the browser, inspect the data that is sent to the server when an item is selected from the dropdown menu:
English to Chinese English to French English to German English to Italian English to Japanese English to Korean English to Portuguese English to Spanish Chinese to English French to English French to German German to English German to French Italian to English Japanese to English Korean to English Portuguese to English Russian to English Spanish to English
Let’s populate these data into our ComboBox: Deselect all items from the stage, and add a new script:
translationmode.addItem(({label:"English to Chinese", data:"en_zh"}));
translationmode.addItem(({label:"English to German", data:"en_de"}));
translationmode.addItem(({label:"English to Italian", data:"en_it"}));
translationmode.addItem(({label:"English to Japanese", data:"en_ja"}));
translationmode.addItem(({label:"English to Korean", data:"en_ko"}));
translationmode.addItem(({label:"English to Portuguese", data:"en_pt"}));
translationmode.addItem(({label:"English to Spanish", data:"en_es"}));
translationmode.addItem(({label:"Chinese to English", data:"zh_en"}));
translationmode.addItem(({label:"French to English", data:"fr_en"}));
translationmode.addItem(({label:"French to German", data:"fr_de"}));
translationmode.addItem(({label:"German to English", data:"de_en"}));
translationmode.addItem(({label:"German to French", data:"de_fr"}));
translationmode.addItem(({label:"Italian to English", data:"it_en"}));
translationmode.addItem(({label:"Japanese to English", data:"ja_en"}));
translationmode.addItem(({label:"Korean to English", data:"ko_en"}));
translationmode.addItem(({label:"Portuguese to English", data:"pt_en"}));
translationmode.addItem(({label:"Russian to English", data:"ru_en"}));
translationmode.addItem(({label:"Spanish to English", data:"es_en"}));
That’s it! Test movie and enter some text, select a translation mode, and click the Translate button to see the translated text! Not too bad is it? Try it out:
Okay, this is what it looks like. Why doesn’t it work? It’s because when the new Flash Player 7 is running inside a browser through HTTP, the new cross-domain security prevents this SWF from loading data from another domain.
What is needed is a policy file – an XML document specifying who is allowed, and this file has to reside at the web service domain. One way to get around this is to use a redirection proxy (server-side script) at this domain, to pass the data between servers and back to this translator. This will be the subject for another day.
UPDATE:
Instead of hardcoding the translation modes into the movie, Mark Shepherd of Macromedia suggested using the XMLConnector component to load an external XML file with the translation modes, and bind the data to the ComboBox. I’ve gone one step further by adding the web service URL in this XML document (BabelFish.xml). This is what I usually do for application configurations, it makes updating quick and easy without going into the source movie and recompiling another SWF.
Also, a redirection proxy file is being used. However, for some reason, this proxy only works when the movie is running inside Flash’s IDE or from the desktop standalone projector, but still not from this domain, even though both the SWF and the proxy are located in the same folder. This is the proxy similar to the one posted at Macromedia, except I’m using JScript instead of VBScript (that does not work either):
Why does this proxy work outside of the browser and not from the same domain? I’d really like to get an answer to this one.
You can download the source files, check them out and let me know if you find anything. Thanks!