Spring Boot Internationalization

Spring Boot Internationalization

A simple tutorial to implement internationalization (i18n) in a Spring Boot application. The idea of internationalization is to make your application adaptable to different languages and regions without engineering changes.

This tutorial will guide you to create a simple Spring Boot application and implement internationalization using LocaleResolver and ResourceBundleMessageSource.

Step 1: Create a New Spring Boot Application

Create a new Spring Boot application if you haven't yet. You can easily do this using Spring Initializr (https://start.spring.io/).

Step 2: Add Necessary Dependency

Ensure that you have the Spring Web dependency in your pom.xml file.

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> 

Step 3: Create Message Properties Files

Create message property files for different languages in the src/main/resources directory. These files will contain the messages in different languages.

For example, create messages.properties for default messages:

greeting = Hello 

And messages_fr.properties for French:

greeting = Bonjour 

Step 4: Configure LocaleResolver and ResourceBundleMessageSource

Next, configure the LocaleResolver and ResourceBundleMessageSource beans in your main application class or a configuration class.

import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import java.util.Locale; @Configuration public class InternationalizationConfig { @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); slr.setDefaultLocale(Locale.US); return slr; } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } } 

Step 5: Create a Controller

Create a controller that uses the MessageSource to get the localized greeting message.

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.util.Locale; @RestController public class HomeController { @Autowired private MessageSource messageSource; @GetMapping("/greeting") public String greeting(HttpServletRequest request) { return messageSource.getMessage("greeting", null, request.getLocale()); } } 

Step 6: Test your Application

You can test your application by running it and visiting http://localhost:8080/greeting. By default, it should display "Hello".

To test French localization, you can change your browser language setting to French and refresh the page. Alternatively, you could use Accept-Language header in your request and set it to fr to see "Bonjour".

That's it! You've implemented basic internationalization in a Spring Boot application. For complex scenarios, you might need to implement a mechanism to allow users to switch languages on the frontend, which would involve sending the Accept-Language header with each request.


More Tags

outliers latex database-deadlocks setvalue python-unittest h.264 assets masstransit python-3.6 modalviewcontroller

More Programming Guides

Other Guides

More Programming Examples