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 is 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 decide which local is currently 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 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 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 the German version of the site

Internationalization in Spring Boot

Internationalization in Spring Boot

Local selection work based on your browser default local, if no locale specified, browser default locale picked by the system.

 

7. Localized Exception Handling 

Spring Boot Internationalization support can be used to provide the localized exception or validation messages.To handle exceptions we can use global exception handler mechanism provided by Spring.

We can define our validation or exception messages in the localized message.properties file.The messages in English are in a file called messages.properties in the src/main/resource folder.For other local specific message sources, use message_language.properties pattern (e.g messages_en.properties).

NotNull.first.name=Please provide the first name.
NotNull.last.name=Please provide the last name.
Exception.notFound=No record of {0} could be found with id {1}.
Exception.unexpected=An unexpected error occurred while processing your request.

Whenever validation fails, Spring Boot will return specific message based on the annotation name.(We will cover this topic in another post)

In above example, Exception.notFound and Exception.unexpected are global messages and they do not belong to any specific entity /object.

 

8. Summary

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

5
Leave a Reply

avatar
3 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
Umesh AwasthidiloasverScotthannes Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
hannes
Guest
hannes

awesome

Umesh Awasthi
Admin
Umesh Awasthi

Thanks!!

Scott
Guest
Scott

Great job! Can you add a unit test example and how to ensure it is selected correctly

diloasver
Guest
diloasver

hello I have a probleme I try to overide public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(localeChangeInterceptor());
} but I have an error . Error method does not override from superclass what I my missing please

Umesh Awasthi
Admin
Umesh Awasthi

You should extend WebMvcConfigurerAdapter in your config class like @Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { … config .. } for SPring 5.x, I will recommend using WebMvcConfigurer as WebMvcConfigurerAdapter is Deprecated.