Solved

Google Translate API v2 in Flash

Posted on 2011-09-28
4
551 Views
Last Modified: 2012-05-12
Hi, I am trying to add a Google translate option to some text in Flash. I want to use the API v2 and have registered, set up payment etc and created a key. I simply want the text in one text box to be translated and then pasted into another text box.

I have two text boxes (txtContent and txtTranslated) and a button with the following code added to it, but I am at the edge of my knowledge now! I get some compile errors when exporting and nothing happens.

If possible I would like all the code to be under the one button as if I get this working I want to copy it to lots of other FLAs. Any help and suggestions appreciated.

Tom
====

on (release) {
      var service:HTTPService = new HTTPService();
      service.url = 'https://www.googleapis.com/language/translate/';
      service.request.v = '2.0';
      service.request.q = txtContent.text;
      service.request.source = 'en'
      service.request.target = 'es'
      service.request.key = 'MY_KEY_HERE;
      service.resultFormat = 'text';
      service.addEventListener(ResultEvent.RESULT, onServerResponse);
      service.send();

private function onServerResponse(event:ResultEvent):void {
  try {
    var json:Object = JSON.decode(event.result as String);
            txtTranslated.text = event.result;
  } catch(ignored:Error) {
  }
0
Comment
Question by:tomdenton
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 36718369
You can use AJAX API in this case you don;t need dev key

var service:HTTPService = new HTTPService();
var url:String = "http://ajax.googleapis.com/ajax/services/language/" +
      "translate?v=1.0&q=" + txtContent.text + "&langpair=" + "en" + "|" + "es";
service.url = url;
service.useProxy = false;
service.resultFormat = 'text';
service.addEventListener(ResultEvent.RESULT, onServerResponse);
service.send();
0
 

Author Comment

by:tomdenton
ID: 36718612
Thanks dgofman, but i wanted to try and move to v2 as it seems that Google are stopping the free service soon??

and also, how would i get the result back into the second text box?

thanks for your help
0
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 36718650
@tomdenton: I don't have DEV_KEY so I cannot test new Google API

That should work as soon you will get result

var json:Object = JSON.decode(event.result as String);
            txtTranslated.text = json["translate"];
0
 

Author Closing Comment

by:tomdenton
ID: 36896971
Sorted, using ajax in the end - thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pairs challenge 5 63
Image decoding from Camera 3 72
Definitions and default visual studio colors 5 58
C# application error "Parameter Is Not Valid". 3 79
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now