Integrating Google Cloud Translation Service with a Spring Boot Application

Ishara Perera
Aeturnum
Published in
8 min readJul 3, 2024

In today’s globalized world, providing content in multiple languages can significantly enhance user engagement and accessibility. Integrating language translation services into your applications can streamline this process. This article will walk you through the steps to integrate the Google Cloud Translation API with a Spring Boot application, enabling you to translate text dynamically within your app.

Why Google Cloud Translation?

Google Translation Service is a powerful tool that provides extensive features to meet various translation needs. Its integration capabilities, support for numerous languages, advanced neural machine translation, and customization options make it an ideal choice for developers and businesses looking to add translation functionalities to their applications.

While Google Translate is a popular and powerful translation service, there are several other competitive services available that offer unique features and capabilities. Some of them are

  • Microsoft Translator
  • Amazon Translate
  • IBM Watson Language Translator
  • DeepL Translator
  • SYSTRAN
  • SDL Language Cloud

Google Translation Service stands out from other translation services for several reasons.

  • Real-Time TranslationInstantly translate text between languages with high accuracy
  • Extensive Language Support Supports over 100 languages, covering most of the world’s widely spoken languages
  • Advanced Neural Machine Translation (NMT) Google uses state-of-the-art Neural Machine Translation models, which provide more accurate and fluent translations compared to traditional phrase-based translation models
  • Integration and Accessibility Provides RESTful APIs, SDKs for different programming languages
  • Security and ComplianceEnsures that your data is handled securely and in compliance with global data protection regulations
  • Ease of Use — Extensive documentation and tutorials

Let’s start

In this article, I will demonstrate the minimal steps needed to get the project up and running in the following areas. You can add additional features and configurations to the Spring Boot application and the Google Cloud project later.

  1. Google Cloud Translation API Setup
    This setup involves creating a new project in the Google Cloud Console, enabling the Translation API, and generating the necessary API credentials
  2. Spring Boot Project Setup
    Configure Spring Boot by setting up a new Spring Boot project, adding the required dependencies, and configuring the application properties
  3. Integrating Google Cloud Translation Service
    Writing a service to interact with the Translation API and creating REST endpoints for translation requests
  4. Running the Application
    Guide to running the Spring Boot application
  5. Testing
    Demonstrating how to test the application with Postman

Google Cloud Translation API Setup

We’ll start by setting up Google Cloud Project.

  • Go to the Google Cloud Console: Visit Google Cloud Console
  • Click on the project dropdown in the top menu and select “New Project
  • Enter a desired name for your project and click “Create

Next, we need to enable the Translation API. To do this, enter “Cloud Translation API” in the search box and select that.

Then, enable this service. Once enabled, it will appear under “Enabled APIs & Services” in your console.

After enabling the service, you’ll need credentials to utilize it. We’ll proceed by creating new credentials specifically for this service.

  • In the left-hand menu, select the “Credentials” option and it will navigate you to the credentials page
  • Click on the “Create Credentials” button at the top. Then select “Service account” from the dropdown menu
  • Next, provide a service account name and press “CREATE AND CONTINUE
  • After that select “Cloud Translation API User” as the role option and complete the flow by pressing “Done
  • Upon completion, the created service account should be visible on the “Service Accounts” page as depicted below
  • To create a key for the newly created service account, navigate to the “Manage keys” option from the “Actions” menu.
  • Proceed by selecting the “Create new key” option from the “ADD KEY” dropdown menu. Then, choose the “JSON” option and click “CREATE”
  • Finally, the newly created private key will be downloaded to your device. Remember to keep it safe as it will be used to facilitate communication between your Java application and the Google Cloud Translate service.

Sample key structure

{
"type": "service_account",
"project_id": "***************-*****",
"private_key_id": "********************",
"private_key": "*************************",
"client_email": "**********@*****-*****.*****.*****.com",
"client_id": "***************",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/*************************-*****.*****.*****.com",
"universe_domain": "googleapis.com"
}

Spring Boot Project Setup

In this section, I will walk you through configuring a Spring Boot application to integrate with the Google Cloud Translation API. Follow these steps to set up your project, add the necessary dependencies, and configure the application properties.

In this example, I will use Spring Initializr to set up the Spring Boot project, Gradle as the build tool, and IntelliJ Community Edition as the IDE. You can use any Java-supported IDE you are comfortable with, such as Spring Tools Suite (STS), Eclipse, Visual Studio Code (VS Code), NetBeans, etc.

Follow these steps to create a new Spring Boot application.

1 . Go to https://start.spring.io/

2 . Fill in the project details

  • Project: Gradle
  • Language: Java
  • Spring Boot Version: 3.3.0 (or select a stable version at the project creation time)
  • Group: organization’s domain name in reverse
  • Artifact: Name of your project
  • Name: A user-friendly name for your application
  • Description: A summary of what your project is about
  • Package Name: Auto-generated based on your group and artifact, but you can customize it if needed
  • Packaging: Jar
  • Java version: 17

3 . Add the following Dependencies

  • Spring Web
  • Lombok

4 . Press the “GENERATE” button and it will download a ZIP file containing your new Spring Boot project.

5 . Extract the contents of the ZIP file to your project directory, and then open it from IntelliJ IDEA.

Once you’ve opened the project in IntelliJ IDEA, ensure the following configurations are set correctly.

File -> Project Structure -> Project Settings -> Project

Integrating Google Cloud Translation Service

Follow these steps to integrate the Google Cloud Translation API into your newly created Spring Boot application.

  • First, you have to add the dependency for the Google Cloud Translation API client library. Open “build.gradle” and add the following. (Make sure to use the latest version available)
implementation group: 'com.google.cloud', name: 'google-cloud-translate', version: '2.43.0'
  • Next, include the following dependency to enable validation.
implementation 'org.springframework.boot:spring-boot-starter-validation'
  • Next, you’ll need to add the Google Cloud Service configurations to your project. To do this, include the following properties in the “application.properties” file located in the “src/main/resources” directory.
google.cloud.project-id=your-project-id
google.cloud.credentials.location=file:/path/to/your-service-account-file.json

Replace “your-project-id” with your actual Google Cloud project ID. This information can be found in the JSON file (private key) you created at the beginning of this article.
Replace “/path/to/your-service-account-file.json” with the path to your downloaded service account JSON file (private key).

  • Then we need to create REST endpoints to expose translation functionality. Therefore, we will implement two controller methods responsible for handling incoming translation requests and delegating them to the translation service for processing. Please refer “LanguageUtilityController.java
  • Next, we need a service class to interact with the Google Cloud Translation API. This class will contain methods to translate text from one language to another and to detect the language of a given text. This service class will utilize a “TranslationServiceClient” for accessing Google Cloud Translation services with the specified credentials. Please refer “GoogleCloudTranslateCredentialsProvider.java
  • All necessary source code for the examples discussed in this article can be found in the following repository.

GitHub

Running the Application

Running a Spring Boot application can be done in a few simple steps, whether you’re using an IDE or the command line.

Using an IDE

1 . STS (Spring Tool Suite)

  • Import the project as a Gradle project.
  • Locate the main application class (LanguageTranslationDemoApplication).
  • Right-click on the class and select Run As -> Java Application.

2 . IntelliJ IDEA

  • Open the project.
  • Locate the main application class.
  • Right-click on the class and select
    Run ‘LanguageTranslationDemoApplication’.

Using Command Line

  • Gradle- Navigate to the root directory of your project (where the build.gradle file is located) and run the following command.
./gradlew bootRun

Alternatively, you can package your application into a JAR file and run it.

./gradlew build
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

Once the application is running, you can access it through a web browser or an API client like Postman. By default, Spring Boot runs on port 8080.

Testing

The “http://localhost:8080/language-service” API offers two essential endpoints: one for detecting the language of a given text and another for translating text from one language to another. By using an API development tool, you can effortlessly send requests to these endpoints and examine the responses. In this demonstration, I used Postman to send requests to a locally running instance of the Language Service API.

1 . Detect Language

2 . Translate Text

Conclusion

Integrating Google Translate into a Spring Boot application is straightforward and can significantly enhance the reach and usability of your application. By following the steps outlined in this article, you can quickly add powerful translation capabilities to your Spring Boot project. Remember to handle the API keys and credentials securely and adhere to best practices for security and privacy.

Happy coding!

--

--