Internationalization in Spring Boot

Introduction to Internationalization in Spring Boot

In this short post, we will explore how to add Internationalization in Spring Boot application.

1. Introduction

Spring Boot provides a number of build in features which help us to start application development quickly. Spring Boot provides ResourceBundleMessageSource which is a foundation to the internationalization provided by Spring as part of the Spring Boot.

We will be using thymeleaf as our front end templating engine.We can enable thymeleaf in Spring Boot application by using spring-boot-starter-thymeleaf.

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

 

2. Application Structure

We will be working with a Maven based Spring Boot application, here is the structure for our internationalization. (it will remain same for Gradle based project). Spring Boot application by default will look for internationalization key and values under /src/main/resources folder.

src/
|--  main/
      |--    resources/
               |--   messages.properties
               |--   messages_de.properties
               |--   messages_xx.properties

 

Default locale file will name as messages.properties and files for other locales will havemessages_xx.properties a format where xx is the locale code. In our case, we have another message file containing German data.

Keys which will be used for internationalization will be same in all the message_xx.properties file and only values will be different based on the locale.

If a key does not exist in a certain requested locale, then the application will fall back to the default locale value. 

Below is our sample message.properties file

Default

welcome.message=Welcome to Demo Application
language.change=Change Language
lang.eng=English
lang.de= German

messages_DE.properties

welcome.message=Willkommen bei der Demo-Bewerbung
change.language=Sprache ändern
lang.eng=Englisch
lang.de= Deutsche

 

3. LocaleResolver

LocalResolver is required to correctly determine which local is currently being used. LocalResolver interface allows for implementations based on a request, session, cookies, etc. The default implementation is AcceptHeaderLocaleResolver. We will be using session based LocalResolver in our sample code. Please read LocaleResolver for more details.

@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.US);
return  localeResolver;
}

 

4.LocaleChangeInterceptor

We need to configure an interceptor which allows for changing the current locale on every request, via a configurable request parameter 

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}

default parameter name used by LocalCangeInterceptor is  “locale” but we will be using “language” as the request parameter.

 

We need to add our LocaleChangeInterceptor with Spring Boot so as this can be picked correctly by Spring Boot. In order to register this bean with Spring Boot, we need to override addInterceptor() method in our Configuration class.

@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(localeChangeInterceptor());
}

5. Controller 

In order to see this in action, we need a controller which will be used to serve a welcome page to see Spring Boot internationalization in action.

@Controller
public class WelcomeController {
@RequestMapping("/")
public String hello() {
return "welcome";
}
}

 

Above Controller will be come in to picture when we open our application home page (in our case it is http://localhost:8080). It will pick welcome.html template located at src/main/resources/templates.

 

6. UI / HTML

Here is our sample HTML 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="#{welcome.message}"></h1>
<span th:text="#{lang.change}"></span>:
<select id="locales">
<option value=""></option>
<option value="en" th:text="#{lang.eng}"></option>
<option value="de" th:text="#{lang.de}"></option>
</select>
</body>
</html>

 

7. Demo Application

If we run our Spring Boot application using main class and open http://localhost:8080

@SpringBootApplication
public class JavadevspringbootApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(JavadevspringbootApplication.class, args);
}
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.US);
return  localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(localeChangeInterceptor());
}
}

We will get following HTML Page

Internationalization in Spring Boot
Internationalization in Spring Boot

 

On changing the URL by adding language in the URL (http://localhost:8080/?language=de), we will get German version of the site

Internationalization in Spring Boot
Internationalization in Spring Boot

 

7. Summary

In this post, we learned how to use Internationalization in Spring Boot.We get an understanding about Spring Boot build in support for Internationalization.

You May Also Like