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.springmvc = Happy learning Spring MVC

File Name:
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”.

 <bean id="messageSource"    
    <property name="basename" value="welcome"/>    

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

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"
<property name="defaultLocale" value="en" />

<bean id="localeChangeInterceptor"
<property name="paramName" value="ln" />

<bean id="annotationMapper"
<property name="interceptors">
<ref bean="localeChangeInterceptor" />

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-

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=”” %>
<h1>Spring MVC internationalization example</h1>
Language : <a href=”?ln=en”>English</a>|<a href=”?ln=zh_CN”>Chinese</a>
welcome.springmvc : <spring:message code=”welcome.springmvc” text=”default text” />


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="" %>

You need to specify key in “code” field.

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

And you are done.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s