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