Guide to REST API Implementing Internationalisation in a Spring Boot Application

yevgenp
4 min readJun 5, 2023

In this tutorial, we will explore the fundamentals of internationalisation and demonstrate how to effectively internationalise your Spring Boot application.

Configuring the Locale Settings

To set the locale for internationalisation in your Spring Boot application, you can follow these steps:

  1. Define the default locale: In your application’s configuration, set the default locale by specifying the desired language and region. This will be used as a fallback when the requested locale is not available.
  2. Implement locale resolution strategy: Determine how the application will resolve the user’s locale. Spring Boot provides various strategies, such as using the Accept-Language header, URL parameters, or cookies. Choose the appropriate strategy based on your application’s requirements.
  3. Use the locale in your code: Once the locale is resolved, you can use it in your code to retrieve the appropriate localised messages, labels, or any other locale-specific content. Spring Boot’s MessageSource provides methods to retrieve messages based on the specified locale.

By properly configuring the locale settings in your Spring Boot application, you can ensure that your application dynamically adapts to different languages and regions, providing a personalised experience for your international users.

application.properties -> set local to UA as default

spring.web.locale: ua

After configuring the locale settings, you can proceed to create a controller in your Spring Boot application and pass the Locale object into the method. This allows you to retrieve and utilize the locale-specific information for that particular request.

Here’s an example of how you can achieve this:

  1. Create a controller class: Begin by creating a new controller class or modifying an existing one.
@RestController
public class MyController {

@Autowired
private MessageSource messageSource;

@GetMapping("/greeting")
public String getGreeting(Locale locale) {
String greeting = messageSource.getMessage("greeting.message", null, locale);
return greeting;
}
}

@Configuration
public class LanguageConfiguration {
private static final String UTF_8 = "UTF-8";
private static final String MESSAGES = "messages";

@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setBasename(MESSAGES);
resourceBundleMessageSource.setDefaultEncoding(UTF_8);
return resourceBundleMessageSource;
}
}
  1. Inject the MessageSource bean: Autowire the MessageSource bean into your controller class. This bean is responsible for retrieving the localised messages.
  2. Pass the Locale object as a method parameter: In the desired request mapping method, include the Locale object as a parameter. This allows Spring Boot to automatically resolve and provide the appropriate locale for that request.
  3. Utilize the locale in your code: Inside the method, you can use the MessageSource to retrieve the localised messages based on the provided Locale. In the example above, the getMessage method is used to fetch the greeting message based on the greeting.message key.

By following these steps, your Spring Boot controller will be able to receive the Locale object, enabling the delivery of localised content and providing a multilingual experience for your users.

Configuring the Resource Bundles

To define various texts for different locales in your Spring Boot application, you can create resource bundles using properties files. Resource bundles are sets of properties files with the same base name and language-specific suffixes. Here’s how you can do it:

  1. Create properties files: For each supported locale, create a properties file with the base name you choose. For example, if you choose the base name “messages”, you can have files like messages_en.properties for English and messages_ua.properties for Ukraine.
  2. Define locale-specific texts: In each properties file, define the key-value pairs for the corresponding locale. The keys should be unique across all properties files, while the values will contain the translated or locale-specific texts.

For example, in messages.properties, you might have:

messages.properties
greeting.message=Привіт!

messages_ua.properties
greeting.message=Привіт!

messages_en.properties
greeting.message=Hello!
  1. Include a default properties file: It’s important to include a default properties file with the same base name as the fallback option. This file should not have any language-specific suffix. For example, messages.properties can serve as the default file, which will be used if a specific locale is not supported.
  2. Place the properties files in the classpath: Ensure that the properties files are placed in the classpath of your Spring Boot application, typically under the src/main/resources directory.

By creating resource bundles with properties files, you can define locale-specific texts for different languages and regions, making your application adaptable to various locales. Spring Boot’s MessageSource will automatically load the appropriate properties file based on the requested locale, providing localised content to your users.

Testing time

curl -X GET http://localhost:8080/greeting \ -H “Accept-Language: ua” \

In this example, we assume that the Spring Boot application is running on localhost at port 8080. The /greeting endpoint is the one that you have defined in your controller to retrieve the localised greeting message.

The -H "Accept-Language: ua" header specifies the desired language and region for the response. Modify this header value according to the specific locale you want to test.

Executing this cURL request will trigger the API and retrieve the localised greeting message based on the specified locale.

Feel free to leave your comments in the section below! Remember to follow me and remember that practice makes perfect!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

yevgenp
yevgenp

Written by yevgenp

Lead Software Engineer | Tech Lead | Software Architect | Senior Software Engineer | IT Career Coach, Mentor & Consultant

No responses yet

Write a response