Spring 3 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.
Popular Spring Tutorials
  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial
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
emp.label.id=Employee Id
emp.label.name=Employee Name
emp.label.age=Employee Age
label.title=Employee Management System
label.footer=© www.dineshonjava.com

emp.label.id=Impelyee Id
emp.label.name=Impelyee Vorname
emp.label.age=Impelyee iage
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 class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="localeChangeInterceptor">
    <property name="paramName" value="lang"></property>
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver">
    <property name="defaultLocale" value="en"></property>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" id="handlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor"></ref>
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:

<%@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>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<spring:message code="emp.label.menu"></spring:message>