Internationalization in Spring Web App

Internationalization : It is the way we design software / application so it could be easily adapted locally (in various languages) without much code changes.

I am going to explore how we can give support of multiple languages to your web application written with Spring.

For our example, lets take two languages English and Chinese, Depending on the user choice, the appropriate language will be selected.

Step 1: We will create two properties file which will contain all the messages to be displayed in the application. These files are kept in a source folder called “resources”.

File Name: welcome.properties
welcome.springmvc = Happy learning Spring MVC


File Name: welcome_zh_CN.properties
welcome.springmvc = \u5feb\u4e50\u5b66\u4e60 Spring MVC

Step 2: Bean Registration for Message Source

We need to create a bean in xml file, which will read messages from properties file.
Where I had set property basename to welcome. Because both properties file in which we
had kept our messages contains basename -welcome, appended with some words like “_zh_CN”.

     <beans>  
 <bean id="messageSource"    
        class="org.springframework.context.support.ResourceBundleMessageSource">    
    <property name="basename" value="welcome"/>    
  </bean>  
 </beans>
    

Step 3: What we are trying to achieve is this-

>> welcome.htm?ln=en – Get the message from English properties file.
>> welcome.htm?ln=zh_CN – Get the message from Chinese properties file-welcome_zh_CN.properties.

So based on parameter “ln” value, Spring should automatically read messages from different files.

This is also called as -locale change.
locale contains information about your current language.

Step 4: Rgisterig bean for Locale resolver

To make Spring MVC application supports the internationalization, you need to register two beans.

1. SessionLocaleResolver
Register a “SessionLocaleResolver” bean, name it as “localeResolver“. It resolves the locales by getting the predefined attribute from user’s session.

2. LocaleChangeInterceptor
Register a “LocaleChangeInterceptor” interceptor and reference it to any handler mapping that need to supports the multiple languages.

<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="ln" />
</bean>  

<bean id="annotationMapper"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>

Some Explanation:
While registering “localeResolver” , I have set property “defaultLocale” to value “en”.
It means by default Spring is going to read messages form file- welcome.properties

And while registering bean “localeChangeInterceptor”, I have set “paramName” to “ln”.
It means based on value “ln” locale will change.

Step 5:
Your JSP page will look like-

<%@ page contentType=”text/html;charset=UTF-8″ %>
<%@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
<html>
<body>
<h1>Spring MVC internationalization example</h1>
Language : <a href=”?ln=en”>English</a>|<a href=”?ln=zh_CN”>Chinese</a>
<h3>
welcome.springmvc : <spring:message code=”welcome.springmvc” text=”default text” />
</h3>
</body>
</html>

Here,

Language :  <a href="?ln=en">English</a>|<a href="?ln=zh_CN">Chinese</a>

when user click on English or Chinese, parameter “ln” is set and locale will change by “localeChangeInterceptor”
And accordingly properties file is read by Spring.

To read value from property file we have used spring tag library.

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
 

You need to specify key in “code” field.

 <spring:message code="welcome.springmvc" text="default text" />

And you are done.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s