Translate API V2 Javascript - Not working

132 views
Skip to first unread message

Rik

unread,
Feb 2, 2012, 9:32:59 AM2/2/12
to Google AJAX APIs
This standard translate API V2 script (from http://code.google.com/apis)
isn't working.
Please could anybody body tell what's wrong, thanks a lot!

<html>
<head>
<title>Translate API Example</title>
</head>
<body>
<div id="sourceText">Hello world, how are you?</div>
<div id="translation"></div>
<script>
function translateText(response) {
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={API ID}&source=en&target=de&callback=translateText&q=' +
sourceText;
newScript.src = source;

document.getElementsByTagName('head')[0].appendChild(newScript);
</script>
</body>
</html>

Jeremy Geerdes

unread,
Feb 2, 2012, 10:44:58 AM2/2/12
to google-ajax...@googlegroups.com
It's virtually impossible to debug code pasted directly into the group because Groups often trashes the formatting and such, wrapping lines and more. Please provide a link to the page that's having trouble or - if that is absolutely impossible - a pastebin of your code.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

> --
> You received this message because you are subscribed to the Google
> Groups "Google AJAX APIs" group.
> To post to this group, send email to
> google-ajax...@googlegroups.com
> To unsubscribe from this group, send email to
> google-ajax-searc...@googlegroups.com
> To view this message on the web, visit
> http://groups.google.com/group/google-ajax-search-api?hl=en_US
> For more options, visit this group at
> http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en

Jean Haidar

unread,
Mar 6, 2012, 4:06:21 PM3/6/12
to google-ajax...@googlegroups.com
I use this following from code inside ASPX page and it worked fine:
put it all in the <HEAD>
 
 

<

head runat="server">

<title>Translate API Example</title>

<div id="sourceText">How are you?</div>

<div id="translation"></div>

<script>

function translateText(response) {

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);

// WARNING: be aware that YOUR-API-KEY inside html is viewable by all your users.

// Restrict your key to designated domains or use a proxy to hide your key

// to avoid misuage by other party.

// Works fine for french

// var source = 'https://www.googleapis.com/language/translate/v2?key=YOURAPIKEY=en&target=fr&callback=translateText&q=' + sourceText;

// for Arabic

var source = 'https://www.googleapis.com/language/translate/v2?key=YOURAPIKEY&source=en&target=ar&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>

</head>
Reply all
Reply to author
Forward
0 new messages