Menu Close

Using Google Translate API in our Node.js App with google-translate-api

The google-translate-api module lets us use the Google Translate API in our server-side JavaScript app.

In this article, we’ll look at how to use this package in our Node.js app.

Installation

We can install the package by running:

npm install @vitalets/google-translate-api

Usage

We can use the translate function that comes with this module to do our translation.

For example, we can write:

const translate = require('@vitalets/google-translate-api');

(async ()=>{
  try {
    const res = await translate('je parle français', { to: 'en' })
    console.log(res.text);
    console.log(res.from.language.iso);
  }
  catch (error){
    console.log(error)
  }
})()

translate returns a promise with the result.

The first argument is the text we want to translate.

The 2nd argument has the to property which is the code of the language we want to translate to.

res.text has the resulting text.

And res.from.language.iso has the code of the language of the text in the first argument.

The Google Translate API works with text that have typos.

For example, we can write:

const translate = require('@vitalets/google-translate-api');

(async ()=>{
  try {
    const res = await translate('I spea French', { from: 'en', to: 'fr' })
    console.log(res.text);
    console.log(res.from.text.autoCorrected);
    console.log(res.from.text.value);
    console.log(res.from.text.didYouMean);
  }
  catch (error){
    console.log(error)
  }
})()

We pass in ‘I spea French’ which has a typo.

from has the language oif the original text.

res.text has the translated result without the typo.

res.from.text.autoCorrected has the boolean to indicate whether the original text was autocorrected.

res.from.text.value has the autocorrected version of the text.

res.from.text.didYouMean is true means the API suggested a correction in the source language.

So we get:

je parle français
true
I [speak] French
false

from the 4 console logs.

We can also add our own language to the list.

For example, we can write:

const translate = require('@vitalets/google-translate-api');
translate.languages['sr-Latn'] = 'Serbian Latin';

(async ()=>{
  try {
    const res = await translate('I spea French', { to: 'sr-Latn' })
    console.log(res.text);
  }
  catch (error){
    console.log(error)
  }
})()

Then we get:

Говорим француски

from the console log.

Proxy

We can make requests via a proxy with this library.

To do this, we write:

const translate = require('@vitalets/google-translate-api');
const tunnel = require('tunnel');

(async ()=>{
  try {
    const res = await translate('I spea French', {
      to: 'fr',
      agent: tunnel.httpsOverHttp({
        proxy: {
          host: '138.68.60.8',
          proxyAuth: 'user:pass',
          port: '8080',
          headers: {
            'User-Agent': 'Node'
          }
        }
      })
    })
    console.log(res.text);
  }
  catch (error){
    console.log(error)
  }
})()

We use the tunnel library to make requests over a proxy.

We call the tunnel.httpOverHttp method with the proxy options and credentials to make the translate request over a proxy.

Conclusion

We can use the google-translate-api module lets us use the Google Translate API in our server-side JavaScript app.

Posted in JavaScript APIs