DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

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

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

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
Enter fullscreen mode Exit fullscreen mode

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)
  }
})()
Enter fullscreen mode Exit fullscreen mode

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)
  }
})()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
  }
})()
Enter fullscreen mode Exit fullscreen mode

Then we get:

Говорим француски
Enter fullscreen mode Exit fullscreen mode

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)
  }
})()
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
danielish profile image
moyilo

Integrating Google Translate API into your Node.js app with the 'google-translate-api' library is a straightforward process. Begin by installing the library using npm:

bash
Copy code
npm install google-translate-api
Next, in your Node.js application, you can use the following example code:

javascript
Copy code
const translate = require('google-translate-api');

translate('Hello, how are you?', { to: 'es' })
.then(res => {
console.log(res.text); // Output: Hola, ¿cómo estás?
})
.catch(err => {
console.error(err);
});
Ensure that you have the required API key from the Google Cloud Console and replace it in the code. This setup enables seamless language translation in your Node.js app.

Additionally, for information related to financial data, you may want to explore Google Finance, a comprehensive platform providing real-time stock quotes, financial news, and market analysis. Incorporating Google Finance data into your app can offer valuable insights for users interested in financial information.