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 several build in features which help us 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 use 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>

This is how the final maven pom.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.javadevjournal</groupId>
   <artifactId>spring-boot-internationalization-application</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>Spring Boot Internationalization example</name>
   <description>Demo project for Spring Boot</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.0.RELEASE</version>
      <relativePath />
      <!-- lookup parent from repository -->
   </parent>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

 

2. Application Structure

We will work 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 for internationalization will be the 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 use 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("lang");
    return localeChangeInterceptor;
}

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

 

We need to add our LocaleChangeInterceptor with Spring Boot so as picked correctly by Spring Boot. 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 

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">
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script></head>
<body>

<h1 th:text="#{welcome.message}"></h1>

<span th:text="#{language.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>
<script type="text/javascript">
    $(document).ready(function () {
        $("#locales").change(function () {
            var selectedOption = $('#locales').val();
            if (selectedOption != '') {
                window.location.replace('?lang=' + selectedOption);
            }
        });
    });
</script>
</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. Video

Here is a short video explaining how to enable Internationalization in Spring Boot application.

 

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. The complete code for this article is available on GitHub.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

11
Leave a Reply

avatar
5 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
nashUmesh AwasthiSimondiloasverScott 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.

SImon
Guest
SImon

How is it possible to change the filename from messages.properties into another name?

Umesh Awasthi
Admin
Umesh Awasthi

Hello Simon,

Can you provide more details as what you are trying to accomplish?

Simon
Guest
Simon

I want to use different property files, each for one html site.
Like page1.html, page1.properties, page2.html, page2.properties, etc.

Umesh Awasthi
Admin
Umesh Awasthi

Not sure what is your requirement, since resources will be combined at the end by Spring and we will have a combined view of all the properties files. There are different ways to configure multiple properties file in Spring, here are some of the options
1. Using a command line argument like spring.config.location.
2. Environment variable
3. ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(Application.class)
.properties("multiple properties file")
.build().run(args);

nash
Guest
nash

In application.properties file you can add a / multiple basenames
spring.messages.basename=i18n/messages, lang/page1, lang/page2

“i18n/messages” is my default but you could add lang/page1 eg.
you need “/resources/lang/page1.properties” file then

trackback
Spring Interview Questions | Java Development Journal

[…] For detail, read Internationalization in Spring Boot […]