In this code, I am sending individual words to google translate in a for loop and am trying to pair the translated responses from the google translate API with the original word. However, I have come to learn the difference between asynchronous and synchronous in the process of doing so!
The problem now is to somehow continue to utilize the efficiency of an asynchronous AJAX call but to somehow capture the translations in the original order that I sent them to google translate.
for (word in original_text) {
$.ajax({
type: "GET",
url: "https://www.googleapis.com/language/translate/v2?",
data: {key: my_Key, source: "en", target: "es", q: original_text[word]},
dataType: 'json',
success: function (result, status, xhr) {
translated.push(result.data.translations[0].translatedText);
alert(translated);
if (translated.length === original_text.length) {
var merged = {};
for (word in original_text) {
merged[original_text[word]] = translated[word];
}
dfrd.resolve(JSON.stringify(merged));
}
},
error: function (xhr, status, errorMsg) {
alert(xhr.status + "::" + xhr.statusText + "::" + xhr.responseText);
}
});