Internationalization

Internationalization

Internationalization is the process of designing Java application on the basis of Locality is called as Internationalization.

Java is providing very good Internationalization support in Java applications due to UNICODE representations and the predefined library like java.util.Locale and some of the predefined classes from java.text package.

To provide Internationalization in Spring WEB MVC applications , Spring has provided very good environment with the following two beans.

  1. SessionLocaleResolver
  2. LocaleChangeInterceptor
1. SessionLocaleResolver

SessionLocaleResolver resolves locales by inspecting a predefined attribute in a userβ€˜s session. If the session attribute doesnβ€˜t exist, this locale resolver determines the default locale from the accept-language HTTP header.

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
   <property name="defaultLocale" value="en" />
</bean>
2. LocaleChangeInterceptor

LocaleChangeInterceptor interceptor detects if a special parameter is present in the current HTTP request. The parameter name can be customized with the paramName property of this interceptor. If such a parameter is present in the current request, this interceptor changes the userβ€˜s locale according to the parameter value.

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
   <property name="paramName" value="lang" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
      <list> 
         <ref bean="localeChangeInterceptor" />
      </list>
   </property>
</bean>

To implement Internationalization in Spring WEB MVC application then we have to use the following steps.

1. Prepare properties file for each and every local to which we want to support.

In Internationalization , we have to prepare a separate properties file with the locale respective messages in the form of Key-Value pairs, where keys must be in English and the values must be in the respective language letters in the form of UNOCODE.

In general, the name format of the properties file is “baseName_lang.properties”.

E.g

messages_en.properties

uname = User Name
upwd = User Password
2. Prepare User forms by getting messages from the properties file

To get messages from properties files we have to use the following tag from Spring tag library with the URI “http://www.springframework.org/tags”.

<spring:message code="--"/>

Where code attribute will take key of the message defined in properties file.

registrationform.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
   <body>
      <form:form method="POST" action="reg" commandName="user">
         <center>
            <table>
               <tr>
                  <td>
                     <spring:message code="uname"/>
                  </td>
                  <td>
                     <form:input path="uname"/>
                  </td>
               </tr>
               <tr>
                  <td>
                     <spring:message code="upwd"/>
                  </td>
                  <td>
                     <form:password path="upwd"/>
                  </td>
               </tr>
               <tr>
                  <td>
                     <spring:message code="uemail"/>
                  </td>
                  <td>
                     <form:input path="uemail"/>
                  </td>
               </tr>
               <tr>
                  <td>
                     <spring:message code="umobile"/>
                  </td>
                  <td>
                     <form:input path="umobile"/>
                  </td>
               </tr>
               <tr>
                  <td><input type="submit" value="Registration"/></td>
               </tr>
            </table>
         </center>
      </form:form>
   </body>
</html>
3. Configure SessionLocaleResolver, LocaleChangeInterceptor and ResourceBundleMessageSource in Spring Configuration File

Where the purpose of SessionLocaleResolver is to resolves locales by inspecting a predefined attribute in a userβ€˜s session. If the session attribute doesnβ€˜t exist, this locale resolver determines the default locale from the accept-language HTTP header.

Where the purpose of “LocaleChangeInterceptor” is to detects if a special parameter is present in the current HTTP request. The parameter name can be customized with the paramName property of this interceptor. If such a parameter is present in the current request, this interceptor changes the userβ€˜s locale according to the parameter value.

Where the purpose of “ResourceBundleMessageSource” is to resolve the properties file on the basis of the provided “basename” property in order to submit values to the User forms.

ds-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config />
   <context:component-scan base-package="com.ashok.spring" />
   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
   </bean>
   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="messages" />
   </bean>
   <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
      <property name="defaultLocale" value="en" />
   </bean>
   <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
      <property name="paramName" value="lang" />
   </bean>
</beans>
Internationalization


Scroll to top