Spring MVC Internationalization (i18n) Tutorial & Example

This tutorial walks you through the step by step instructions in order to support internationalization (i18n) in Spring MVC. Please see Spring MVC Web Application With Example to setup Spring MVC basic example. 

login.jsp


Let's create a sample login.jsp which will use <spring:message> tag to display labels.

<!-- Spring message tag for i18n -->
        <td><spring:message code="label.username"/></td>
        <td><input type="text" name="username" /></td> 
    </tr>
    <tr>
        <td><spring:message code="label.password"/></td>
        <td><input type="text" name="password" /></td>
    </tr>
     <tr>
        <td colspan="2">
            <input type="submit" value="<spring:message code="label.login"/>"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

For the purposes of the example, lets assume the properties are defined in messages.properties. This file should be in the classpath. Following entry should be made in the spring configuration xml file in order to use messages.properties. (NOTE : Please define the different versions of this file i.e. messages_en_US.properties, messages_cs_CZ.properties depending on the supported locales.)


<bean id="messageSource"
 class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
 <!-- If you have multiple properties file (depending on usecases), 
  use 'basenames' property and provide multiple properties file separated by 
  comma -->
 <property name="basename" value="classpath:messages" />
</bean>


messages.properties


label.firstname=First Name
label.lastname=Last Name
label.login=Login







Local Resolvers

A locale resolver is a component capable of resolving the locale a client is using, in order to be able to offer internationalized views. Locale resolvers and interceptors are all defined in the org.springframework.web.servlet.i18n package, andare configured in your application context in the normal way. Here is a selection of the locale resolvers included in Spring.


  • AcceptHeaderLocaleResolver
This locale resolver inspects the accept-language header in the request that was sent by the browser of the client. Usually this header field contains the locale of the client's operating system. This is the default locale resolver and you don't need to configure anything in spring configuration xml file in order to use this locale resolver.


  • CookieLocaleResolver
This locale resolver inspects a Cookie that might exist on the client, to see if a locale is specified. If so, it uses that specific locale.


<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">

  • SessionLocaleResolver
 It resolves the locales by getting the predefined attribute from user’s session.

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">

  • LocaleChangeInterceptor
You can build in changing of locales using the LocaleChangeInterceptor. This interceptor needs to be added to one of the handler mappings. It will detect a parameter in the request and change the locale (it calls setLocale() on the LocaleResolver that also exists in the context). All calls containing a parameter named siteLanguage will now change the locale. So a request for the following URL, http://www.mycompany.net/myapp/login.jsp?siteLanguage=nl will change the site language to Dutch.


<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  <property name="paramName" value="siteLanguage" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
  <property name="interceptors">
    <list>
      <ref bean="localeChangeInterceptor" />
    </list>
  </property>
 </bean>

2 comments: