How to Use Amazon Translate in a Spring Boot Application

  • 4.1/5
  • 169
  • Feb 13, 2025
In this article, we will see how to use Amazon Translate in a Spring Boot application. Amazon Translate is a cloud-based machine translation service provided by AWS, allowing developers to translate text between different languages programmatically.

1) Add AWS SDK Dependencies

To integrate Amazon Translate, we need the AWS SDK for Java. Add the following dependencies in your pom.xml:
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>translate</artifactId>
            <version>2.30.17</version>
        </dependency>
After adding all the required dependencies, the complete pom.xml should look something like this:



2) Configure AWS Credentials

Amazon Translate requires authentication. You can configure AWS credentials in application.yml as shown below:



3) Implement Translation Service

The TranslationService class loads AWS credentials and the region from the configuration file, ensuring secure authentication with Amazon Translate. It initializes a TranslateClient using these credentials, allowing seamless interaction with the AWS Translate service.

Additionally, it provides a method to translate text between languages by constructing a TranslateTextRequest, sending it to Amazon Translate, and returning the translated output.


4) Create a REST Controller

The TranslateController class is a Spring MVC controller that handles translation requests. It injects the TranslationService using @Autowired and exposes a /translate endpoint via @PostMapping.

When a request is received with text, sourceLang, and targetLang parameters, it calls the translateText method from TranslationService and returns the translated text as the response.



5) Test the REST API

To test the REST API, you can use Postman, cURL, or Swagger UI. To test using cURL, run the following command in your terminal:
curl -X POST http://localhost:8080/translate \
     -d "text=Hello, how are you?" \
     -d "sourceLang=en" \
     -d "targetLang=es"
Hola, ¿cómo estás?

6) Translate UI

This HTML file provides a user interface for translating text using Amazon Translate. It uses Bootstrap for styling and jQuery for handling user interactions. The form allows users to input text, select a source and target language, and submit the request.

When the "Translate" button is clicked, an AJAX request is sent to the /translate endpoint, and the translated text is displayed dynamically. Bootstrap ensures a responsive and visually appealing layout, while jQuery simplifies form handling and request processing.



7) Run and Test the Application

Start your Spring Boot application and open http://localhost:8080/ in your browser. Enter text, select languages, and click Translate to see the result.



Source Code: GitHub
Index
Create and Run a React app and JSX Basics

14 min

Building a Translation Application with Spring Boot, OpenAI (ChatGPT), Thymeleaf, and jQuery

6 min

How to Use Amazon Translate in a Spring Boot Application

7 min

How to Download AWS S3 Content Using AWS CLI on a Mac Machine

2 min

Cryptography: Encoding, Encryption, and Hashing (Concept, Use Cases, and Examples)

26 min

Load Testing using Locust.io (Open-source)

2 min

How to create a Multi-Module Project in Maven

2 min

Building RESTful APIs with Node.js, Express & MongoDB

14 min

Docker commands every developer should know

13 min

AWS S3 integration with Spring Boot (AWS S3 + Spring Boot)

14 min