Olivier Salmon
Olivier Salmon

Reputation: 11

Google Translate API and Node.js

I wonder if someone could help here. I can successfully display the translation into the console log, but can't manage to get it into a variable.

Code is:

// Imports the Google Cloud client library
const Translate = require('@google-cloud/translate');
// Your Google Cloud Platform project ID
const projectId = '';
// Instantiates a client
const translate = new Translate({
  projectId: projectId,
});

function translateTo(textToTranslate,target){
    translate
        .translate(textToTranslate, target)
        .then(results => {
            var translation = results[0];
            console.log(`Text: ${textToTranslate}`);
            console.log(`Translation: ${translation}`);
        })
        .catch(err => {
            console.error('ERROR:', err);
        });
}

var txtTranslated = translateTo(data.results[0].alternatives[0].transcript,'fr')
console.log('Txt Translated: ' + txtTranslated)

txtTranslated is always empty :-(

Not sure if it helps or not but the function translateTo() is called within a class

Thank you

Upvotes: 0

Views: 1840

Answers (2)

Olivier Salmon
Olivier Salmon

Reputation: 11

sendTranscription(data){
  // use .then() to wait for the promise to resolve before continuing
  this.translateTo(data.results[0].alternatives[0].transcript,'fr').then(function(txtTranslated) {
 console.log('Txt Translated: ' + txtTranslated)
  var response = {
    text: data.results[0].alternatives[0].transcript,
    languageCode: this.request.config.languageCode,
    user: this.user,
    textTranslated: txtTranslated
  }
  for (var i = 0; i < clients.length; i++) {
    clients[i].send(JSON.stringify(response));
    console.log(JSON.stringify(response));
  }
}.bind(this));
}

// Translate
translateTo(textToTranslate,target){
  // Return here to return the promise
  return translate
      .translate(textToTranslate, target)
      .then(results => {
        var translation = results[0];
        console.log('Text to translate :' + textToTranslate);
        console.log('Translation: ' + translation);
        return translation;
    })
    .catch(err => {
        console.error('ERROR:', err);
    });
}

Upvotes: 0

Harmonic
Harmonic

Reputation: 377

It looks to me like the problem is that the function translateTo is not actually returning anything. Return the translation and it will be stored in txtTranslated.

     function translateTo(textToTranslate, target) {
       // ceate a new var to store the result
       var translation = null;
       translate
         .translate(textToTranslate, target)
         .then(results => {
           translation = results[0];
           return translation;
         })
         .catch(err => {
           console.error('ERROR:', err);
         });
       // this should now the translation:
       console.log(translation)
       return translation;
     }

     var txtTranslated = translateTo(data.results[0].alternatives[0].transcript, 'fr')
     console.log('Txt Translated: ' + txtTranslated)

If the console.log before the return is null,then the problem is Async - as in we are not waiting for the translate response before returning the translation in the function.

Upvotes: 1

Related Questions