【发布时间】:2018-02-26 21:35:40
【问题描述】:
我想知道是否有人可以在这里提供帮助。 我可以成功地将翻译显示到控制台日志中,但无法将其转换为变量。
代码是:
// 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 总是空的 :-(
不确定它是否有帮助,但函数 translateTo() 在类中被调用
谢谢
【问题讨论】:
-
您能详细说明您的问题吗?您是说 txtTranslated 会出现在
console.log()中,对吗?那么你不能在变量中得到它是什么意思? -
console.log(
Translation: ${translation});将显示翻译好的文本。但在功能之外,我无法获得翻译。这个调用函数 var txtTranslated = translateTo(data.results[0].alternatives[0].transcript,'fr') 并且 txtTranslated 是空的,所以 console.log('Txt Translated: ' + txtTranslated) 什么都不显示
标签: node.js google-cloud-platform