Use what's called a method closure. Essentially, it's a function that returns a
function. You can use it to pass your callback additional arguments which will
help you match your translation requests up with the original strings. For
instance, check this out:
var strings = [
'These are the strings to translate.',
'There are several of them.',
'They will be run in a loop.',
'And we\'ll pass the index to the callback so it can match them up.'
];
function callback(index, response){
if(response.translatedText){
alert(strings[i] + ' -> ' + response.translatedText);
}
}
function createClosure(context, method, args){
return function(){
for(var i=0; i<arguments.length; i++){
args.push(arguments[i]);
}
return method.apply(context, args);
}
}
for(var i=0; i<strings.length; i++){
google.language.translate(strings[i], '', 'es', createClosure(window,
callback, [i]);
}
Here's what's happening (barring typos, etc.):
We set up an array of strings to translate
We define the callback that we want, which will take two arguments: the index
of the original string in the strings array and the response from the API.
We define the method closure, which will take three arguments: the scope in
which we want to run the callback (e.g., if you have a translate control
context), the method you want to call later, and the arguments that you want to
pass it.
The closure will take these three arguments and build a function that will have
the arguments we want to pass in scope when it's called. This function will
then add the arguments it receives to the ones we want to send and pass it all
to your callback.
We loop through the strings array and run the translations.
Now, this code is just off the top of my head. There could be issues with it
yet, so I'm not guaranteeing it will work right out of the gate. But it should
demonstrate essentially what you need to do.
Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA
For more information or a project quote:
[email protected]
If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!
On Apr 28, 2011, at 1:47 AM, dan wrote:
> Hi, sometimes I need to call translate API for more target languages
> in loop. The problem is that callback function is called
> asynchronously. That's obvious, but I need that kind of identifier is
> supplied in response object in order to be able match requests and
> results. The best would be an additional query parameter defined by
> programmer which become a part of result then.
>
> Or is there any way how to pair request/response already now?
>
> Thanks,
> Dan
> NewLink s.r.o.
>
> --
> 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.