Spring 3 MVC Internationalization

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Spring 3 MVC Internationalization & Localization with Example

Posted by Dinesh Rajput


In this chapter we will discuss about Internationalization (I18N) and Localization (L10N) in
Spring 3.0 MVC.

What is i18n and L10n?


In computing, internationalization and localization are means of adapting computer software to
different languages and regional differences. Internationalization is the process of designing a
software application so that it can be adapted to various languages and regions without
engineering changes. Localization is the process of adapting internationalized software for a
specific region or language by adding locale-specific components and translating text.
The terms are frequently abbreviated to the numerous i18n (where 18 stands for the number of
letters between the first i and last n in internationalization) and L10n respectively, due to the
length of the words. The capital L in L10n helps to distinguish it from the lowercase i in i18n.
Now lets we create an application which support multiple languages. We will add two languages
support to our application: English and German. Depending on the locale setting of users
browser, the appropriate language will be selected.
Step 1: First we create the properties file for the different languages.
1. messages_en.properties
2. messages_de.properties
resources/messages_en.properties
view plainprint?
1. emp.label.id=Employee Id
2. emp.label.name=Employee Name
3. emp.label.age=Employee Age
4. emp.label.salary=Salary
5. emp.label.address=Address
6.
7. label.menu=Menu
8. label.title=Employee Management System

9.
10. label.footer= www.dineshonjava.com
resources/messages_de.properties
view plainprint?
1. emp.label.id=Impelyee Id
2. emp.label.name=Impelyee Vorname
3. emp.label.age=Impelyee iage
4. emp.label.salary=shalery
5. emp.label.address=Adrrezz
6.
7. label.menu=Men
8. label.title=Impelyee Managemenot Sistom
9.
10. label.footer= www.dineshonjava.com
Step 2: Configure i18n & l10n to Spring3MVC
we need to declare these files in spring configuration file. We will use class
org.springframework.context.support.ReloadableResourceBundleMessageSource to define the
message resources. Also, note that we will provide a feature where user will be able to select
language for the application. This is implemented by using
org.springframework.web.servlet.i18n.LocaleChangeInterceptor class. The
LocaleChangeInterceptor class will intercept any changes in the locale. These changes are then
saved in cookies for future request.
org.springframework.web.servlet.i18n.CookieLocaleResolverclass will be used to store the
locale changes in cookies.
Add following code in the sdnext-servlet.xml file.
view plainprint?
1. <bean class="org.springframework.context.support.ReloadableResourceBundleMessageS
ource" id="messageSource">
2.

<property name="basename" value="classpath:messages"></property>

3.

<property name="defaultEncoding" value="UTF-8"></property>

4. </bean>
5.
6. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="local
eChangeInterceptor">
7.

<property name="paramName" value="lang"></property>

8. </bean>
9.
10. <bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeR
esolver">
11.

<property name="defaultLocale" value="en"></property>

12. </bean>
13.
14. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle
rMapping" id="handlerMapping">
15.
16.
17.

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

18. </bean>
Note that in above configuration we have defined basename property in messageSource bean to
classpath:messages. By this, spring will identify that the message resource message_ will be
used in this application.
Change in the JSPs Template:
WebRoot/WEB-INF/views/header.jsp
view plainprint?
1. <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2.

3. <h3><spring:message code="emp.label.title"></spring:message></h3><span style="float


: right;">
4.

<a href="?lang=en">en</a>

5.

6.

<a href="?lang=de">de</a>

7. </span>
WebContent/WEB-INF/views/menu.jsp
view plainprint?
1. <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2.
3. <spring:message code="emp.label.menu"></spring:message>

You might also like