Spring MVC Internationalization
Internationalization (i18n) in Spring MVC allows you to create applications that can be easily adapted to different languages and regions. This guide covers the key concepts and steps for implementing internationalization in Spring MVC, including configuring message sources, setting up locale resolution, and creating locale-specific message files.
Key Concepts of Internationalization
- MessageSource: An interface that defines the contract for resolving messages, with support for the parameterization and internationalization of such messages.
- ResourceBundleMessageSource: An implementation of the
MessageSource
interface that accesses resource bundles using specified basenames. - LocaleResolver: An interface that resolves the locale from the request.
- SessionLocaleResolver: An implementation of
LocaleResolver
that uses a locale attribute in the user's session. - LocaleChangeInterceptor: An interceptor that allows for the locale to be changed via a request parameter.
Configuring Message Sources
Configure a MessageSource
bean to resolve messages from resource bundles:
WebConfig.java
// WebConfig.java
package com.example.springmvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
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 java.util.Locale;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
source.setDefaultEncoding("UTF-8");
return source;
}
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.ENGLISH);
return resolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
return interceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
Creating Locale-Specific Message Files
Create properties files for different locales in the src/main/resources
directory:
messages.properties (Default)
# messages.properties
greeting=Hello
messages_fr.properties (French)
# messages_fr.properties
greeting=Bonjour
messages_es.properties (Spanish)
# messages_es.properties
greeting=Hola
Creating the Controller
Create a controller to handle requests and demonstrate internationalization:
HomeController.java
// HomeController.java
package com.example.springmvc.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Locale;
@Controller
public class HomeController {
@Autowired
private MessageSource messageSource;
@GetMapping("/home")
public String home(Locale locale, Model model) {
String greeting = messageSource.getMessage("greeting", null, locale);
model.addAttribute("greeting", greeting);
return "home";
}
}
Creating the View
Create a JSP page to display the localized message:
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h2>${greeting}</h2>
<a href="?lang=en">English</a>
<a href="?lang=fr">French</a>
<a href="?lang=es">Spanish</a>
</body>
</html>
Testing Internationalization
To test internationalization, start the Spring MVC application and access the /home
URL with different lang
parameters:
http://localhost:8080/home?lang=en
- Should display "Hello"http://localhost:8080/home?lang=fr
- Should display "Bonjour"http://localhost:8080/home?lang=es
- Should display "Hola"
Key Points
- MessageSource: An interface that defines the contract for resolving messages with support for parameterization and internationalization.
- ResourceBundleMessageSource: An implementation of
MessageSource
that accesses resource bundles using specified basenames. - LocaleResolver: An interface that resolves the locale from the request.
- SessionLocaleResolver: An implementation of
LocaleResolver
that uses a locale attribute in the user's session. - LocaleChangeInterceptor: An interceptor that allows for the locale to be changed via a request parameter.
- Configure a
MessageSource
bean to resolve messages from resource bundles. - Create properties files for different locales to store localized messages.
- Create a controller to demonstrate internationalization and use the
MessageSource
to get localized messages. - Create a view to display localized messages and provide links to switch between locales.
Conclusion
Internationalization (i18n) in Spring MVC allows you to create applications that can be easily adapted to different languages and regions. By configuring message sources, setting up locale resolution, and creating locale-specific message files, developers can provide a seamless and localized user experience in their Spring MVC applications. Happy coding!