ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

Translate Language In Node JS Using AWS Translate

Translate Language In Node JS Using AWS Translate
Translate Language In Node JS Using AWS Translate

In this article, we will learn how to translate language in node js using AWS translate. The Amazon Translate service provides high-quality translations on demand by using advanced machine learning technologies. Amazon Translate can be used to translate unstructured text documents or to build multilingual applications. Amazon Translate does not charge you for translations in which the source language and target language are the same. An Amazon Translate service is based on neural networks trained to translate languages. With this feature, you can translate text between a source language (the original language of the text being translated) and a target language (the language into which the text is being translated).

Prerequisites

  1. AWS account
  2. A basic understanding of Node JS

How to translate language in node js using AWS translate?

Project Setup

Open the terminal and create a new directory named language-translate and switch to that directory using the following command.

mkdir language-translate
cd language-translate

Inside the language-translate directory, let’s initialize a node project using npm init command.

After initializing a pacakge.json file will be generated like the image provided below.

translate language in node js

Now, let’s install the required dependencies required to export the data in an excel file.

npm install express aws-sdk

This will install express and aws-sdk modules that will be used for routing and translating functionality of AWS will be used with the help of aws-sdk.

Next, let’s create a index.js file where we will create a route through which translation will happen.

#index.js

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get("/translate", async (req, res) => {
  // translation logic
}); 
app.listen(3000, function(){
  console.log("listening on port 3000");
});

The above code will initialize the express framework, set up the server at port 3000, and create a route translate that will be used for language translation.

Language Translation

In this step, let’s create a file where we will write a logic to translate text from one language to another. Let the name of the file be translate.js which will be located inside root folder.

# translate.js

var AWS = require('aws-sdk');
const translate = new AWS.Translate({
  accessKeyId: 'aws access key' // Replace with your aws access key
  secretAccessKey: 'aws secret key', // Replace with your aws secret key
  region: 'aws region', // Replace with your aws region
});

async function languageTranslate(text) {
  const params = {
    SourceLanguageCode: 'en', // english language code
    TargetLanguageCode: 'de', // german language code
    Text: text
  }

const response = await translate.translateText(params).promise();
  return response;
}

module.exports = {
  languageTranslate,
};

Here, we have used AWS translate functionality to translate the English text into german text. In above code

SourceLanguageCode: determines the language from which provided text will be translated

TargetLanguageCode: determines the language in which provided text will be translated

Text: the text which will be translated from the source language to the target language

The above code block will translate English text to german. If you do not know the source language, AWS has also provided the solution for that. In this case, you have to set SourceLanguageCode to auto instead of any language code. Then, AWS itself will determine, the source and translate it into the target language.

Now, let’s update our index.js file, so that when accessing the /translate route the text provided through the query will be translated into german and returned to the user.

const express = require('express');
const { languageTranslate } = require('./translate')

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get("/translate", async (req, res) => {
  const textToTranslate = req.query.text;
  const response = await languageTranslate(textToTranslate);
    return res.status(200).json({
      message: "Executed successfully",
      data: response.TranslatedText
    });
});

app.listen(3000, function () {
  console.log("listening on port 3000");
});

Now, if you access the route localhost:3000/translate with the query parameter text=Hello you will get the following output.

Conclusion

By following the above process, we can easily translate language in node js using AWS translate functionality.

Sign up for daily dose of tech articles at your inbox.
Loading