Friday, December 13, 2013

Internationalization Spring MVC with Java Config

I will post parts of code from an application to show how to integrate this feature using the new current in Spring: Java Configurations. Sometimes it can be a challenge to update your old xml config to this pretty new config type.



1. Create in WEB-INF folder a new folder messages and add to this folder some files with a base name and suffix(without suffix is the default file).

2. Add to these files, key-value records like:

                 facebook.displayName=English Facebook -> messages.properties
                 facebook.displayName=China Facebook   -> messages_zh_CN.properties

    *You can also add other suffixes like fr for French

3. Now, i think it's time for some Java Config:)


@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
 
 @Bean 
 public LocaleChangeInterceptor localeChangeInterceptor(){
     LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
     localeChangeInterceptor.setParamName("language");
     return localeChangeInterceptor;
 }

 @Bean(name = "localeResolver")
 public LocaleResolver sessionLocaleResolver(){
     SessionLocaleResolver localeResolver=new SessionLocaleResolver();
     localeResolver.setDefaultLocale(new Locale("en"));
     
     return localeResolver;
 }  
 
 public void addInterceptors(InterceptorRegistry registry) {
     registry.addInterceptor(localeChangeInterceptor());
 }
 
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/WEB-INF/messages/messages");
        return messageSource;
    }
    
    
   //...
}

* Note that "SessionLocalResolver" has a name. I don't know why, but declaring this without a name caused me some problems

* "language" will be the parameter that will have to be in request url for getting values from a different file

       **language=fr will load values from messages_fr

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"

      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

    <head>

        <title>Spring Security Example </title>

    </head>

    <body>

 <p th:text="#{facebook.displayName}">Welcome to our grocery store!</p>

</body>

</html>
Because we have only two files, we have only 2 choises:

*call without paramater will result:


*call with ?language=zh_CN:



I think the most important part and unknown part is Java Config, others are the same as in older spring versions. This kind of implementation is also a beginning for adding other custom interceptors. You can implement HandlerInterceptor  and create a bean that will return an instance, than register that bean in addInterceptors method and will work. Before we did this between <mvc:interceptors> tags in xml configs. I can't say which alternative is the best, both are great because we talk about Spring:D OOP, Java, groovy, java frameworks, java language, Spring, internationaliztion, Java Config, Spring 3, Spring Java Config

3 comments:

  1. Very helpful. You have explained well using java config for internalization.

    ReplyDelete
  2. If you are handling localization projects, my recommendation is to try a software localization tool like https://poeditor.com/

    ReplyDelete