Solved

Google Translate API v2 in Flash

Posted on 2011-09-28
4
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
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.

696 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