Spring MVC

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago