Internationalization (i18n) is the process of developing products or services that can be adapted to specific local languages and cultures, a process known as localization (l10n).
Spring Boot makes it easy to add i18n support to your application with the help of the spring-boot-starter-data-jpa
and spring-boot-starter-thymeleaf
starters.
In this post, we'll go over how to add i18n support to a Spring Boot application. We'll cover the following topics:
First, we need to add the spring-boot-starter-data-jpa
and spring-boot-starter-thymeleaf
starters to our pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
Next, we need to configure the MessageSource
bean in our application.properties
file:
spring.messages.basename=messages
The MessageSource
bean is responsible for resolving messages from message files. By default, Spring Boot will look for message files in the src/main/resources
directory.
Next, we need to create message files for each language that we want to support. The message file must be named messages_[lang].properties
, where [lang]
is the ISO 639-1 code for the language.
For example, if we wanted to support English and French, we would create the following message files:
messages_en.properties
messages_fr.properties
Each message file will contain key-value pairs for each message that can be resolved by the MessageSource
bean. For example, our messages_en.properties
file might look like this:
greeting=Hello, world!
And our messages_fr.properties
file might look like this:
greeting=Bonjour, monde!
Now that we have our message files set up, we can start displaying localized messages in our views.
In Thymeleaf, we can use the #{...}
syntax to resolve messages from our message files. For example, if we wanted to display the greeting
message from our messages_en.properties
file, we could do the following:
<p th:text="#{greeting}">Hello, world!</p>
If we wanted to display the greeting
message from our messages_fr.properties
file, we could do the following:
<p th:text="#{greeting}">Bonjour, monde!</p>
Now that we can display localized messages in our views, we need a way to switch between languages.
There are many ways to do this, but one simple way is to add a lang
parameter to our application's queryString
. For example, if we wanted to switch to the French language, we could do the following:
http://localhost:8080/?lang=fr
We can then use the lang
parameter to set the Locale
in our LocaleResolver
bean. For example, we could add the following to our application.properties
file:
spring.mvc.locale-resolver=org.springframework.web.servlet.i18n.SessionLocaleResolver
And then add the following to our LocaleResolver
bean:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
return localeResolver;
}
Now, when we visit http://localhost:8080/?lang=fr
, our application will switch to the French language and display the greeting
message from our messages_fr.properties
file.
In this post, we've covered how to add i18n support to a Spring Boot application. We've seen how to configure Spring Boot for i18n, how to create language-specific message files, how to display localized messages in our views, and how to switch languages in our application.