Solved

Google Translate API v2 in Flash

Posted on 2011-09-28
4
562 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

825 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