Google Translate AS 3.0 for Flex developers

Filed under 3.0, Actionscript, Experiments, Flex, Flex 3, Google, Labs

Google is begging amazed by Flash Player capabilities and exposing their services to next-level. After my experience with Google Maps, I took a little time today afternoon to explore any other service that exposes REST results in their API.
There’s some products of google that already exposes REST format like Search, Notes, Contacts, Translate Tool.

To work with REST(is same as JSON) the folks at Adobe did a handy class for it witch you can find here.

here’s a full code example of uses, you have to download and install the as3corelib on your project. Or if you think you’re lazy or don’t have to it. Just download the full project source code here.

Example Running, click on image to launch.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="626">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;		
			import com.adobe.serialization.json.JSON;
 
			[Bindable]public var googleText:String = "http://ajax.googleapis.com/ajax/services/language/" ;
			[Bindable]public var tsrc:String ="translate?v=1.0&q=";
			[Bindable]public var lanpair:String = "&langpair=";
			[Bindable]public var pairCode:String = "%7C";
 
			private function onJSONLoad(event:ResultEvent):void
			{
				var rawData:String = String(event.result);
				if(JSON.decode(rawData).responseData.translatedText !=null){
				var decoded:String =JSON.decode(rawData).responseData.translatedText;
				}else {
					decoded = "None support";
				}
				txtdest.text = decoded;
			}		
 
		]]>
	</mx:Script>
	<mx:Array id="langFrom">
		<mx:Object  value="auto" label="Select Language"/>
		<mx:Object  value="ar" label="Arabic"/>
		<mx:Object  value="bg" label="Bulgarian"/>
		<mx:Object  value="zh-CN" label="Chinese"/>
		<mx:Object  value="hr" label="Croatian"/>
		<mx:Object  value="cs" label="Czech"/>
		<mx:Object  value="da" label="Danish"/>
		<mx:Object  value="nl" label="Dutch"/>
		<mx:Object  value="en" label="English"/>
		<mx:Object  value="fi" label="Finnish"/>
		<mx:Object  value="fr" label="French"/>
		<mx:Object  value="de" label="German"/>
		<mx:Object  value="el" label="Greek"/>
		<mx:Object  value="hi" label="Hindi"/>
		<mx:Object  value="it" label="Italian"/>
		<mx:Object  value="ja" label="Japanese"/>
		<mx:Object  value="ko" label="Korean"/>
		<mx:Object  value="no" label="Norwegian"/>
		<mx:Object  value="pl" label="Polish"/>
		<mx:Object  value="pt" label="Portuguese"/>
		<mx:Object  value="ru" label="Russian"/>
		<mx:Object  value="es" label="Spanish"/>
		<mx:Object  value="sv" label="Swedish"/>
	</mx:Array>
	<mx:Array id="langTo">
		<mx:Object  value="ar" label="Arabic"/>
		<mx:Object  value="bg" label="Bulgarian"/>
		<mx:Object  value="zh-CN" label="Chinese (Simplified)"/>
		<mx:Object  value="zh-TW" label="Chinese (Traditional)"/>
		<mx:Object  value="hr" label="Croatian"/>
		<mx:Object  value="cs" label="Czech"/>
		<mx:Object  value="da" label="Danish"/>
		<mx:Object  value="nl" label="Dutch"/>
		<mx:Object  value="en" label="English"/>
		<mx:Object  value="fi" label="Finnish"/>
		<mx:Object  value="fr" label="French"/>
		<mx:Object  value="de" label="German"/>
		<mx:Object  value="el" label="Greek"/>
		<mx:Object  value="hi" label="Hindi"/>
		<mx:Object  value="it" label="Italian"/>
		<mx:Object  value="ja" label="Japanese"/>
		<mx:Object  value="ko" label="Korean"/>
		<mx:Object  value="no" label="Norwegian"/>
		<mx:Object  value="pl" label="Polish"/>
		<mx:Object  value="pt" label="Portuguese"/>
		<mx:Object  value="ru" label="Russian"/>
		<mx:Object  value="es" label="Spanish"/>
		<mx:Object  value="sv" label="Swedish"/>
	</mx:Array>
	<mx:ComboBox id="langFromCbx" prompt="Select a language" x="28.5" y="64" dataProvider="{langFrom}"/>
 
	<mx:HTTPService id="googleTransService"
		 url="{googleText + tsrc+txtsource.text + lanpair + String(langFromCbx.selectedItem.value)+pairCode+String(langToCbx.selectedItem.value)}" 
		 useProxy="false" showBusyCursor="true" result="onJSONLoad(event)"  resultFormat="text"/>
	<mx:TextArea id="txtsource" x="28.5" y="124" width="569" height="116"/>
	<mx:TextArea id="txtdest" x="28.5" y="274" width="569" height="248"/>
	<mx:Button x="473.5" y="61" label="Translate" click="googleTransService.send()" width="124" height="28"/>
	<mx:ComboBox id="langToCbx" x="222.5" y="64" prompt="Select a language" dataProvider="{langTo}"></mx:ComboBox>
	<mx:Label x="30.5" y="45" text="Translate from:"/>
	<mx:Label x="28.5" y="98" text="Text to Translate"/>
	<mx:Label x="28.5" y="248" text="Result Text translated"/>
	<mx:Label x="222.5" y="45" text="Translate To:"/>
 
</mx:Application>

7 Comments

  1. Posted May 21, 2008 at 8:23 pm | Permalink

    REST != JSON. REST is a network software architecture style. The format of data doesn’t need to be JSON. Some REST services use XML.

  2. Posted May 21, 2008 at 9:09 pm | Permalink

    Yep you right, premature analysis to docs on google .

    REST is completely different from JSON.

    Thanks to point me ;)

  3. vidyadhar
    Posted May 23, 2008 at 10:08 am | Permalink

    I m not able to display the english text into hindi text…..
    could help me…..

  4. OGG
    Posted December 9, 2009 at 12:19 pm | Permalink

    Hi vidyadhar!
    I had similar problem but with other language, try to use AS escape/unescape

  5. Pierre Jarbon
    Posted February 22, 2010 at 3:52 pm | Permalink

    Hey,

    Thanks for the example using the REST api, I found a library that also contained the Google Translation services, and much more AND all returned in code-friendly data objects:
    http://code.google.com/p/googleas3api/

    Hope I could help!

  6. Posted May 19, 2010 at 4:27 am | Permalink

    Hello,
    Exerimenting some troubles with accents and special chars when translating from spanish to another language. I just tryed to send the params with POST and it works very good.

    1.0
    {txtsource.text} {String(langFromCbx.selectedItem.value)+’|'+String(langToCbx.selectedItem.value)}

    And use this function to call the service. With a little trick with the param v.

    private function translate():void{
    if(txtsource.text==”"){
    txtdest.text=”";
    }
    else
    {
    googleTransService.request.v=”1.0″;
    googleTransService.send();
    }
    }

  7. Posted May 19, 2010 at 4:32 am | Permalink

    Last commment update:

    private function translate():void{
    if(txtsource.text==”"){
    txtdest.text=”";
    }
    else
    {
    googleTransService.method=”POST”;
    googleTransService.request.v=”1.0″;
    googleTransService.request.q=txtsource.text;
    googleTransService.request.langpair=String(langFromCbx.selectedItem.value)+’|'+String(langToCbx.selectedItem.value);
    googleTransService.send();
    }
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*