【问题标题】:Google Translate API and Node.js谷歌翻译 API 和 Node.js
【发布时间】: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


【解决方案1】:

在我看来,问题在于函数 translateTo 实际上并没有返回任何东西。返回翻译,它将存储在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)

如果返回之前的console.log为null,那么问题是异步的——因为我们在函数中返回翻译之前没有等待翻译响应。

【讨论】:

  • 谢谢。我试过使用返回。但它似乎没有将变量的内容传回。我有:文本翻译:未定义
  • 请看看我的编辑,让我知道会发生什么
  • 感谢谐波的帮助。你是对的。这是因为它是异步的(node.js 是异步的,我不知道)。
【解决方案2】:
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);
    });
}

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2011-03-28
    • 2016-11-18
    • 2011-12-26
    • 2010-10-10
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多