NodeJS google-翻译-api BAD_REQUEST

NodeJS google-translate-api BAD_REQUEST

所以给出的例子如下

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

translate('Ik spreek Engels', {to: 'en'}).then(res => {
    console.log(res.text);
    //=> I speak English
    console.log(res.from.language.iso);
    //=> nl
}).catch(err => {
    console.error(err);
});

出现以下错误信息

{ Error at /var/www/translate/node_modules/google-translate-api/index.js:105:17 at at process._tickCallback (internal/process/next_tick.js:160:7) code: 'BAD_REQUEST' }

这是基本设置,如果有人解决了这个问题,请post - 感谢您的帮助。

我建议您使用 official client library from Google Cloud. Be aware though, that there is no free quota 进行翻译 API。示例代码如下所示:

const {Translate} = require('@google-cloud/translate');

const projectId = 'YOUR_PROJECT_ID';

const translate = new Translate({   projectId: projectId, });

const text = 'Hello, world!'; 

const target = 'ru';

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