Mitu Vinci
Mitu Vinci

Reputation: 465

Issues while working with google-translate-api library for Node.js

I am a beginner to javascript.I am trying to detect language and translate language using google-translate-api

I want to use google translate API for free that's why I want to use it. But when I run any code like this below,

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

I get this error.

{ Error
    at F:\Extensionproject\testTranslate\node_modules\google-translate-api\index.js:105:17
    at process._tickCallback (internal/process/next_tick.js:68:7) code: 'BAD_REQUEST' }

I have tried more google translate API library from GitHub. None of this working. I tried this library too! another google-translate-api library But getting this.

{ HTTPError
    at translate (F:\Extensionproject\testTranslate\node_modules\@k3rn31p4nic\google-translate-api\src\index.js:148:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  name: 'HTTPError',
  statusCode: 429,
  statusMessage: 'Too Many Requests' } 

How can I fix this problem?

Upvotes: 2

Views: 5126

Answers (1)

BSeitkazin
BSeitkazin

Reputation: 3059

If you look at library github page - https://github.com/matheuss/google-translate-api, you could see that project is outdated, and there a lot of issues, with the same as yours - https://github.com/matheuss/google-translate-api/issues/70.

I recommend you to use official Google's Translate API. You can find how to use Node JS with Google Translate API here.

How to use it:

First of all, you need to install the library:

npm install @google-cloud/translate

Then, to use it:

async function someFunction() {
    // Imports the Google Cloud client library
    const {Translate} = require('@google-cloud/translate');

    // Instantiates a client
    const translate = new Translate({projectId});

    // The text to translate
    const text = 'Hello, world!';

    // The target language
    const target = 'fr';

    // Translates some text into French
    const [translation] = await translate.translate(text, target);
    console.log(`Text: ${text}`);
    console.log(`Translation: ${translation}`);
}

Upvotes: 2

Related Questions