Spring Boot Internationalization

Spring Boot Internationalization with Examples

In this article, I am going to discuss Spring Boot Internationalization with Examples. Please read our previous article where we discussed Spring Boot CORS Support with Examples.

What is Internationalization?

Internationalization is a process that makes an application run in multiple languages. In other words, this makes the application adaptable to multiple languages without the need of changing the source code. For example, if a person wants to view a webpage in French, rather than English, they must be able to do so easily.

How to Implement Internationalization in Spring Boot?

We do this by creating multiple language files. We shall start with a fresh project.

Step 1: Create a new project using Spring Initializr in VS Code. Remember to include the spring-boot-starter-web and spring-boot-starter-thymeleaf dependencies.

Step 2: Create a new class called WebController.java in src/main/java/com/dotnet/internationalisation directory. This file will be the controller for the application.

Step 3: Modify the newly created WebController.java as follows:

How to Implement Internationalization in Spring Boot?

Step 4: Create another file called Internationalisation.java in

src/main/java/com/dotnet/internationalisation directory. This file shall facilitate the change in language.

Step 5: Import the following packages into the newly created Internationalisation.java:

How to Implement Internationalization in Spring Boot?

Step 6: Add the following functions to the file:

Spring Boot Internationalization with Examples

Each of these functions is used for the following:

  1. localeResolver(): Used to resolve which language needs to be displayed.
  2. localeChangeInterceptor(): The parameter name set in this function (in this case, “lang”) can be used to change the language of the webpage.
  3. addInterceptors(): This adds an interceptor to check the webpage language before displaying the webpage.

Step 7: Create a file called messages.properties in src/main/resources directory. Populate it with a few messages:

Spring Boot Internationalization with Examples

Step 8: Create a file called messages_fr.properties in src/main/resources directory (fr stands for French). Populate it with the same messages as above but in French.

Spring Boot Internationalization with Examples

You may use other languages as well. For German, write as below:

Spring Boot Internationalization with Examples

Step 9: Create a file called locale.html in the src/main/resources/templates directory. This file shall have the structure of the webpage.

Spring Boot Internationalization

Step 10: Write the HTML file as follows:

Spring Boot Internationalization

Step 11: Compile and execute the application. Ensure the compilation is successful.

Spring Boot Internationalization

Step 12: Log on to http://localhost:8080/locale to see the website in English.

Spring Boot Internationalization

Step 12: Log on to http://localhost:8080/locale?lang=fr to see the website in French.

Spring Boot Internationalization

Step 13: Log on to http://localhost:8080/locale?lang=de to see the website in German.

Spring Boot Internationalization

Congratulations! You now know how to implement internationalization in Spring Boot and code your application in multiple languages.

The Complete Example Code
Internationalisation.java
package com.dotnet.internationalisation;

import java.util.Locale;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.context.annotation.Bean;

@Configuration
public class Internationalisation implements WebMvcConfigurer
{
    @Bean
    public LocaleResolver localeResolver()
    {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        sessionLocaleResolver.setDefaultLocale(Locale.US);
        return sessionLocaleResolver;
    }

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

    @Override
    public void addInterceptors (InterceptorRegistry registry)
    {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
InternationalisationApplication.java
package com.dotnet.internationalisation;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class InternationalisationApplication {

    public static void main(String[] args) {
        SpringApplication.run(InternationalisationApplication.class, args);
    }

}
WebController.java
package com.dotnet.internationalisation;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebController
{
    @RequestMapping(value = "/locale")
    public String locale()
    {
        return "locale";
    }
}
locale.html
<!DOCTYPE html>
<html>
    <head>
        <title>Internationalisation</title>
    </head>
    <body>
        <h1 th:text = "#{welcome.text}"></h1>
        <h2 th:text = "#{other1.text}"></h2>
        <h2 th:text = "#{other2.text}"></h2>
    </body>
</html>
messages_de.properties
welcome.text="Hallo! Willkommen!"
other1.text="Dies läuft im Spring Boot."
other2.text="Sie können diese Webseite in anderen Sprachen anzeigen!"
messages_fr.properties
welcome.text="Salut! Accueillir!"
other1.text="Cela s'exécute dans Spring Boot."
other2.text="Vous pouvez consulter cette page Web dans d'autres langues!"
messages.properties
welcome.text="Hi! Welcome!"
other1.text="This is running in Spring Boot."
other2.text="You can view this webpage in other languages!"

In the next article, I am going to discuss Spring Boot HTTPS with Examples. Here, in this article, I try to explain Spring Boot Internationalization with Examples. I hope you enjoy this Spring Boot Internationalisation article.

Leave a Reply

Your email address will not be published. Required fields are marked *