Google translate version 2 flash actionsctipt method

With some great help from some of you guys already, I have some Flash Actionscript that i can use with Google Translate API version 1.0 that works exaclty as i want it to. However, i am hoping to convert the script to work with version 2 of the API as Google is depreciateing the old version due to 'abuse' hmm!

The new URL and format for the API v2 is:
"https://www.googleapis.com/language/translate/v2?key=MY_KEY_HERE&source=en&target=es&q=Hello%20World"

(I will paste my key if needed for testing as i can then change it)

This works fine if pasted into a browser, but not when used place of the URL in the code , which is as follows (it takes English text from a  textbox and puts back the translated version in the same place):

As always, suggestion much appreciated.
var result_lv:LoadVars = new LoadVars();
	result_lv.onLoad = function(success:Boolean) {
		  if (success) {
				var str:String = unescape(this);
				var translatedText:String = str.split('":"')[1].split('"}, ')[0];
				if(translatedText != undefind)
				txtContent.text = translatedText.split('\\r').join('\n').split('\\"').join("'"); 
		  } else {
				trace("Error");
		  }
	};
	var lorem_lv:LoadVars = new LoadVars();
	var from:String = "en";
	var to:String = "es"
	lorem_lv.v = "1.0";
	lorem_lv.format = "text";
	lorem_lv.q = txtContent.text;
	lorem_lv.langpair = from + "|" + to;
	lorem_lv.sendAndLoad("http://ajax.googleapis.com/ajax/services/language/translate", result_lv, "POST");

Open in new window

tomdentonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

conagramanCommented:
Does it error on u? if so any error/warning message?
0
tomdentonAuthor Commented:
No error  but the result gives 'undefined' if I add a trace

To try different things I have reduced the code to what is below to keep it simple (and then add textboxes and other nice stuff later). same undefined result.

If I paste the url into a browser it returns:
{
 "data": {
  "translations": [
   {
    "translatedText": "Hola Mundo"
   }
  ]
 }
}


on (release) {

	var result_lv:LoadVars = new LoadVars();
	result_lv.onLoad = function(success:Boolean) {
		  if (success) {
			trace(lorem_lv.translatedText);
	  } else {
			trace("Error");
		  }
	};
	var lorem_lv:LoadVars = new LoadVars();
	lorem_lv.sendAndLoad("https://www.googleapis.com/language/translate/v2?key=MY_KEY&source=en&target=es&q=Hello%20World", result_lv, "POST");
}

Open in new window

0
conagramanCommented:
Undefined simply means that you are using a variable that is undefined.  Make sure all of your variables are defined and that you have all of your imports.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

dgofmanCommented:
The problem I don't have a key and cannot test a google results
0
tomdentonAuthor Commented:
Hi,

I have pasted my key in for testing purposes - and will change it later of course!

Your help as always is appreciated
on (release) {

	var result_lv:LoadVars = new LoadVars();
	result_lv.onLoad = function(success:Boolean) {
		  if (success) {
			trace(lorem_lv.translatedText);
	  } else {
			trace("Error");
		  }
	};
	var lorem_lv:LoadVars = new LoadVars();
	lorem_lv.sendAndLoad("https://www.googleapis.com/language/translate/v2?key=IzaSyDJ-BQtCn4kW_olCGuOIY27O0JbTSEyk2w&source=en&target=es&q=Hello%20World", result_lv, "POST");
}

Open in new window

0
dgofmanCommented:
I don't see right know HTTPS or HTTP request is working, may be google shutdown a server or disable new API's

http://code.google.com/apis/language/translate/v2/getting_started.html

Try to get from a browser first or JavaScript
0
dgofmanCommented:
<html> 
<head>
<title>Translate API Example</title> 
</head>
<body>
<div id="sourceText">Hello world</div>
<div id="translation"></div>
<script>
function translateText(response) {
if(response["error"])
	alert(response["error"].code + ": "+ response["error"].message);
else
	document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
}
</script>
<script> 
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
var sourceText = escape(document.getElementById("sourceText").innerHTML);
var source = 'https://www.googleapis.com/language/translate/v2?key=IzaSyDJ-BQtCn4kW_olCGuOIY27O0JbTSEyk2w&source=en&target=de&callback=translateText&q=' + sourceText; 
newScript.src = source;
// When we add this script to the head, the request is sent off. 
document.getElementsByTagName('head')[0].appendChild(newScript);
</script>
</body>
</html>

Open in new window

0
dgofmanCommented:
Or maybe your key is invalid
0
tomdentonAuthor Commented:
If i use the following in a browser it returns a result - i think i must have missed part of the key when pasting it in sorry!

https://www.googleapis.com/language/translate/v2?key=AIzaSyDJ-BQtCn4kW_olCGuOIY27O0JbTSEyk2w&source=en&target=es&callback=translateText&q=' + sourceText

The corrected code i am trying to make work in Flash is below. The html/script you suggested does work ok with the corrected key
on (release) {

	var result_lv:LoadVars = new LoadVars();
	result_lv.onLoad = function(success:Boolean) {
		  if (success) {
			trace(lorem_lv.translatedText);
	  } else {
			trace("Error");
		  }
	};
	var lorem_lv:LoadVars = new LoadVars();
	lorem_lv.sendAndLoad("https://www.googleapis.com/language/translate/v2?key=AIzaSyDJ-BQtCn4kW_olCGuOIY27O0JbTSEyk2w&source=en&target=es&q=Hello%20World", result_lv, "POST");
}

Open in new window

0
dgofmanCommented:
I fixed new API code but if you will use ActionScript 3 you will not need manually parse JSON

	var result_lv:LoadVars = new LoadVars();
	result_lv.onData = function(str:String) {
		var lines:Array = str.split('\n');
		for(var l:Number = 0; l < lines.length; l++){
			var line:Array = lines[l].split('"translatedText": "');
			if(line.length == 2){
				var translatedText:String = line[1].substring(0, line[1].length - 1); // remove last "
				trace(translatedText);
				break;
			}
		}
	};
	var lorem_lv:LoadVars = new LoadVars();
	lorem_lv.key = "AIzaSyDJ-BQtCn4kW_olCGuOIY27O0JbTSEyk2w";
	lorem_lv.source = "en";
	lorem_lv.target = "es";
	lorem_lv.q = "Hello World";
	lorem_lv.sendAndLoad("https://www.googleapis.com/language/translate/v2", result_lv, "GET");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomdentonAuthor Commented:
dgofman, you are my hero :-) thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.