Solved

Google Translate API v2 in Flash

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hasOne  challenge 59 90
 shows up in Outlook, not OWA or on phone 3 58
noX challenge 17 76
countPairs challenge 7 58
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
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 tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

18 Experts available now in Live!

Get 1:1 Help Now