Solved

Google Translate API v2 in Flash

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
This is about my first experience with programming Arduino.
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
Starting up a Project

728 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