Skip to main content

How to Use Amazon Translate in a Spring Boot Application

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

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...