Spring MVC Internationalization & Localization with Example

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

emp.label.id=Employee Id
emp.label.name=Employee Name
emp.label.age=Employee Age
emp.label.salary=Salary
emp.label.address=Address
 
label.menu=Menu
label.title=Employee Management System
 
label.footer=© www.dineshonjava.com

resources/messages_de.properties

emp.label.id=Impelyee Id
emp.label.name=Impelyee Vorname
emp.label.age=Impelyee iage
emp.label.salary=shalery
emp.label.address=Adrrezz
 
label.menu=Menü
label.title=Impelyee Managemenot Sistom
 
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.

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
    <property name="basename" value="classpath:messages"></property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>
 
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="localeChangeInterceptor">
    <property name="paramName" value="lang"></property>
</bean>
 
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver">
    <property name="defaultLocale" value="en"></property>
</bean>
 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" id="handlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor"></ref>
    </property>
</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

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

<spring:message code=”emp.label.title”></spring:message>

<span style=”float: right;”> <a href=”?lang=en”>en</a> | <a href=”?lang=de”>de</a> </span>

WebContent/WEB-INF/views/menu.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
 
<spring:message code="emp.label.menu"></spring:message>

 

Previous
Next

3 Comments

  1. Anonymous November 7, 2013
  2. Eduardo González December 17, 2013
  3. Anonymous February 5, 2014