Michael Nelles
Michael Nelles

Reputation: 5992

NodeJS google-translate-api BAD_REQUEST

So the example given is as follows

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);
});

With the following error message

{ 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' }

This is a basic set up if someone has resolved this issue please post - thank you for your assistance.

Upvotes: 5

Views: 2501

Answers (1)

Philipp Sh
Philipp Sh

Reputation: 997

I would advise you to use the official client library from Google Cloud. Be aware though, that there is no free quota for the Translate API. The sample code would look as follows:

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);   
    });

Upvotes: 4

Related Questions