In the previous tutorial, I showed you how to implement internationalization in Spring MVC applications. For Spring Boot applications, how to implement internationalization? I will guide you in this tutorial!
First, I will create a new Spring Boot project:
with Spring Web dependency:
as an example.
Result:
I will configure it to use InternalResourceViewResolver in this example.
I will declare tomcat-embed-jasper dependency to render JSP files:
1 2 3 4 5 |
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> |
and JSTL dependency to work with JSP page:
1 2 3 4 |
<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> |
Next, I will create a new HelloControler:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.springboot.internationalization; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String home() { return "home"; } } |
to display the home.jsp view file located in the src/main/webapp/WEB-INF/views directory with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ page session="false" contentType="text/html; charset=UTF-8" %> <html> <head> <title>Home</title> </head> <body> <a href="<c:url value="?lang=vi"/>"> <spring:message code="app.lang.vi" /> </a> | <a href="<c:url value="?lang=en"/>"> <spring:message code="app.lang.en" /> </a> <h1> <spring:message code="app.welcome" /> ! </h1> </body> </html> |
Here, I have declared to use <spring:message /> to work with internationalization in Spring framework. Please see the tutorial Internationalization in Spring MVC to understand more about it!
By default, Spring Boot will use the Tomcat server to run, so you should note that to display the correct language, we need to declare the contentType with a charset encoding UTF-8:
1 |
<%@ page session="false" contentType="text/html; charset=UTF-8" %> |
Looks like other runtime servers don’t need it!
And I also need to declare in the application.properties file as follows:
1 2 |
spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp |
Now, we will declare the necessary Spring beans for internationalization: messageSource, localeResolver and localeChangeInterceptor.
To configure the messageSource, I will create 2 new files messages.properties and messages_vi.properties in the src/main/resources/i18n directory with the contents of each file as follows:
messages.properties:
1 2 3 |
app.lang.vi=Vietnamese app.lang.en=English app.welcome=Hello world |
messages_vi.properties
1 2 3 |
app.lang.vi=Ti\u1EBFng Vi\u1EC7t app.lang.en=Ti\u1EBFng Anh app.welcome=Xin chào |
Spring Boot supports auto configuration for messageSource with ResourceBundleMessageSource implementation. You can declare some of the following properties:
to use Spring Boot’s default configuration.
For example, I can declare a property:
1 |
spring.messages.basename=classspath:i18n/messages |
to declare a messageSource for my example application.
Here, I will not use the default configuration and will declare the bean to use the ReloadableResourceBundleMessageSource class as follows:
1 2 3 4 5 6 7 |
@Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:i18n/messages"); return messageSource; } |
For localResolver I will also use CookieLocalResolver:
1 2 3 4 |
@Bean public LocaleResolver localeResolver() { return new CookieLocaleResolver(); } |
The localeChangeInterceptor will be configured as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.springboot.internationalization; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; @Component public class InterceptorConfiguration implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } } |
Here, I am using WebMvcConfigurer to add LocaleChangeInterceptor to my application.
Run the app and click between languages to see the results!
Mine is as follows:
and