If I was going to do this, I would do it something like this:
function translate_all(){
var an_array=[
{
text : "Text 1 to translate",
srcLang : "source_language1",
destLang : "dest_language1"
},
{
text : "Text 2 to translate",
destLang : "dest_language2"
}
];
var myClosure=function(a){
return function(result){
if(result.error){
alert('There was a problem translating "' + a.text + '"');
return
}
a.translation = result.translation;
var div = document.createElement('div');
div.appendChild(document.createTextNode(a.translation));
document.getElementsByTagName('body')[0].appendChild(div);
}
};
for(var i=0; i<an_array.length; i++){
var toTrans = an_array[i];
google.language.translate(toTrans.text, toTrans.srcLang || '',
toTrans.destLang, myClosure(toTrans))
}
}
A couple of things you'll notice about this code:
1. instead of using a multi-dimensional array, I used an array of
objects. It makes it a little easier to keep track of what's what.
2. I implemented a closure so you could keep track of what you're
doing when the translations come back. A closure is essentially a
function that returns a function which will remember where you were
and what you were doing when you created it, so it can pick up at that
point again.
3. Instead of using document.write, I used DOM methods to create a div
element, put the translated string inside it, and add that to the
document. This is essential because, after the onload event fires,
document.write will have unpredictable and almost-always disastrous
effects on your page.
4. When the translation returns, it will be assigned back to the
original object as a translation property, so you'll be able to access
an_array[0].translation.
And that's that. Hope it helps. If you have questions, feel free to
post again.
Jeremy R. Geerdes
Effective website design & development
Des Moines, IA
For more information or a project quote:
http://jgeerdes.home.mchsi.com
http://jgeerdes.blogspot.com
http://jgeerdes.wordpress.com
jgee...@mchsi.com
Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.
If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!
And check out my blog, Adventures in Web Development, at
http://jgeerdes.blogspot.com
!