Well, in order to do what you're wanting to do, you're going to need to do some 
Javascript magic. Because you have to wait for translation to return and the 
callback to fire before you can expect your second array to be populated, you 
have to figure out a way to alert the second array in the callback, but only 
when it's completely populated. So we need a check to make sure it's populated. 
Check out the code on the page linked below. It uses what's called a method 
closure to preserve some basic state information between iterations. This 
allows us to make sure that the translated array retains the order of the 
original array. It also employs a simple check to make sure that the translated 
array is completely populated before it fires the final alert. You'll notice 
that only one alert fires, and the translated order should be the same as the 
originalStrings array.

http://savedbythegoog.appspot.com/?id=a8642c89e444c4b3e3b92965d0a35c39a31539c8

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
[email protected]

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

On Jun 29, 2010, at 9:06 AM, Alistair wrote:

> Thanks for your help. I guess I should have explained what else I'm
> trying to do.
> I'm trying to translate values from an array and add the results to a
> new array.
> This is the script I tried:
> 
> google.load("language", "1");
> $(document).ready(function(){
>       var array = ["book","house","dog"];
>       var array2 = [];
>       var l = array.length;
>       for(i=0;i<l;i++){
>               function translate(){
>                       var key = array[i];
>                       google.language.translate(key,"en","ru", 
> function(result)
> { array2[i] = result.translation; });
> 
>               }
>       }
>       alert(array2);
> });
> 
> 
> However, the alert comes back as blank. I can't really include it
> within either of the functions because the for loop has to complete
> first. Perhaps I'm still putting things in the wrong place. Is it
> possible to do what I'm trying to do?
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Google AJAX APIs" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-ajax-search-api?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Google AJAX APIs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-ajax-search-api?hl=en.

Reply via email to