2

Below is my code. I am trying to translate my text to German language and assign to the variable after translating, but I getting an empty string. What mistake am I making while executing the below code?

I am seeking a correction so that after execution the translatedString should have a result of "Hallo ich liebe deutsche Sprache".

I want to use translatedString outside the translate function so that the result should be global:

// working with string translation

const translate = require('google-translate-api');

var myString = "Hello I love German language";
        
console.log("My String is "+ myString); 
// result is My String is Hello I love German language
        
var translatedString = translate(myString, {to: 'de'})
       .then(res => {translatedString = res.text});
        
console.log("Translated String is "+ JSON.stringify(translatedString)); 
// result is  Translated String is {}
// expected result is Translated String is Hallo ich liebe deutsche Sprache
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

3

Translate is returning a promise. A promise represents an eventual value; the translation will not be available right away. If you want to interact with the eventual value, you need to put your code in the promise's .then method.

const translate = require('google-translate-api');

let myString = "Hello I love German language";
console.log("My String is "+ myString); 

let promise = translate(myString, {to: 'de'}).then(res => {
    return res.text;
});

promise.then(translatedString => {
    console.log("Translated String is "+ JSON.stringify(translatedString)); 
});

console.log('The translation has started, but it isn\'t ready yet');

EDIT: As requested, here is a version that saves it to a variable. This is not what i recommend:

const translate = require('google-translate-api');

let myString = "Hello I love German language";
console.log("My String is "+ myString); 

let translatedString = null;
let promise = translate(myString, {to: 'de'}).then(res => {
    translatedString = res.text;
});

promise.then(() => {
    console.log("Translated String is "+ JSON.stringify(translatedString)); 
});
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • I want to use translatedString outside the translate function so that the result shout be global – Purushotham Kumar Sep 29 '17 at 19:05
  • It's impossible for the translatedString to be available before the promise resolves. To wait for the promise to resolve, you put your code in the .then block. Any code you put *outside* the .then block will not wait for the promise, and therefore can not have the translatedString. – Nicholas Tower Sep 29 '17 at 19:10
  • is there any possible way to make the code or refactor the code to wait until translatedString is ready and then assign to it so that i can use it outside translate function and use as globally – Purushotham Kumar Sep 29 '17 at 19:34
  • If you want to wait, you must use .then. It's possible to assign it to some global variable while you're inside the .then. But any other code that wants to use that global variable will either need to use .then as well in order to wait, or that code needs to understand that the value will probably not exist yet. – Nicholas Tower Sep 29 '17 at 20:04
  • can you please refactor the same code so that i assign global variable in .then and use the same code outside the function. I will be very thankful to you if you can help me – Purushotham Kumar Sep 30 '17 at 17:28
  • I have added that version. I do not recommend it. The whole point of a promise is to represent the eventual value. By saving it to a variable, you force the promise to just indicate that that the value is available, but not actually have the value in it. – Nicholas Tower Sep 30 '17 at 17:38
0

Because you are logging the variable before receiving the result while you should log it when the promise is resolved:

translate(myString,  {to: 'de'}).then(res => {
   console.log(res.text); //Hallo ich liebe deutsche Sprache
});
Khalid
  • 4,730
  • 5
  • 27
  • 50
0

The last console.log() call is outside of the Promise chain. You can use .then() to return the Promise value to next .then() in Promise chain or async/await to get the value of the Promise at the next line within an async function

const translatePhrase = async(myString) => {

  const translate = require('google-translate-api');

  // working with string translation

  console.log("My String is "+ myString); 
  // result is My String is Hello I love German language

  const translatedString = await translate(myString, {to: 'de'}).then(res => res.text);

  console.log("Translated String is "+ JSON.stringify(translatedString)); 

}

translatePhrase("Hello I love German language");
guest271314
  • 1
  • 15
  • 104
  • 177