Internationalization, also known as i18n, is the process of designing and developing applications that can be easily translated into multiple languages without requiring code changes. The Spring Framework provides built-in support for i18n through its MessageSource interface. In a Spring MVC application, you can implement i18n by following these steps:
1. Declare MessageSource bean in configuration file:
@Configuration
public class AppConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
}
2. Create messages_XX.properties files where XX represents the locale for each language you want to support. For example, if you want to support English and French, you would create messages_en.properties and messages_fr.properties files. These files should be placed in the root of the classpath.
Example messages_en.properties file:
greeting = Hello!
farewell = Goodbye!
Example messages_fr.properties file:
greeting = Bonjour!
farewell = Au revoir!
3. Use MessageSource in your Spring MVC Controllers to get the translated messages:
@Controller
public class MyController {
@Autowired
private MessageSource messageSource;
@RequestMapping("/hello")
public ModelAndView hello(Locale locale) {
String greeting = messageSource.getMessage("greeting", null, locale);
return new ModelAndView("hello", "greeting", greeting);
}
@RequestMapping("/goodbye")
public ModelAndView goodbye(Locale locale) {
String farewell = messageSource.getMessage("farewell", null, locale);
return new ModelAndView("goodbye", "farewell", farewell);
}
}
In the above example, the MessageSource is injected into the controller using the @Autowired annotation. The getMessage() method is used to retrieve the translated messages for the given locale. The first argument is the key of the message in the properties file, the second is an array of objects that can be used to replace placeholders in the message, and the third is the locale.
4. Use Spring’s view resolver to resolve views based on the locale:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private MessageSource messageSource;
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ResourceBundleViewResolver viewResolver = new ResourceBundleViewResolver();
viewResolver.setBasename("views");
viewResolver.setViewClass(JstlView.class);
viewResolver.setOrder(1);
viewResolver.setExposeContextBeansAsAttributes(true);
viewResolver.setMessageSource(messageSource);
registry.viewResolver(viewResolver);
}
}
In this example, the ResourceBundleViewResolver is used to resolve views based on the locale. The views are defined in messages_XX.properties files located in the root of the classpath. The view resolver sets the order to 1 to ensure that it is used before other view resolvers. The setExposeContextBeansAsAttributes method is used to make the MessageSource bean available in the JSP page using the $messageSource attribute.
5. Use Spring’s <spring:message> tag in JSP pages to display translated messages:
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
<title><spring:message code="title" /></title>
</head>
<body>
<h1><spring:message code="greeting" /></h1>
<p><spring:message code="intro" /></p>
<a href="<c:url value="/goodbye" />"><spring:message code="link" /></a>
</body>
</html>
In the above example, the <spring:message> tag is used to retrieve the translated messages from the MessageSource bean. The code attribute specifies the key of the message in the properties file. The <c:url> tag is used to add context path to link.
These are the steps to implement internationalization in a Spring MVC application. By following these steps, your application will be able to support multiple languages seamlessly.