Spring 3 MVC: Internationalization & Localization Tutorial with Example

Welcome to Part 5 for Spring 3.0 MVC Series. In previous article we saw how to configure Tiles framework with Spring 3 MVC application. We used org.springframework.web.servlet.view.tiles2.TilesConfigurer class in bean definition to define the tiles configuration file. This divided our HelloWorld Spring MVC application in sections such as header, footer etc.

In this part we will discuss about Internationalization (I18N) and Localization (L10N) in Spring 3.0 MVC. We will add i18n support followed by l10n to our HelloWorld Spring application that we created in previous tutorials in this series. I strongly recommend you to go through previous articles and download the source code of our sample application.

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 numeronyms 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.

Our Goal

Our goal is to add Internationalization and Localization support to our Spring MVC application. Once finished our app will look like.
contact-manager-screen-de

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. Also user will be able to select the language from top-right corner of the application.

Message Resouces File

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”. Create a source folder in your project by Right click on Project name > New > Source Folder and name it resources.
message-resources-properties-spring-mvc

Create two files messages_en.properties and messages_de.properties in this folder and copy following content into it.

File: resources/messages_en.properties

label.firstname=First Name
label.lastname=Last Name
label.email=Email
label.telephone=Telephone
label.addcontact=Add Contact

label.menu=Menu
label.title=Contact Manager


label.footer=© ViralPatel.net

File: resources/messages_de.properties

label.firstname=Vorname
label.lastname=Familiename
label.email=Email
label.telephone=Telefon
label.addcontact=Addieren Kontakt 

label.title=Kontakt Manager
label.menu=Menü


label.footer=© ViralPatel.net

Configuring Internationalization (i18n) / Localization (L10n) in Spring MVC

Now we have created message resource properties for our application. 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.CookieLocaleResolver class will be used to store the locale changes in cookies.

Add following code in the spring-servlet.xml file.

File:WebContent/WEB-INF/spring-servlet.xml

	
		
		
	

	
		
	

	
		
	

	
		
			
		
	

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 the View – The JSPs

Now as we have created two message resources files and configured it in Spring MVC, we will use these messages in the JSP files. Open all the JSP files of our demo application and update with following code.

File:WebContent/WEB-INF/jsp/header.jsp

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

en | de

File:WebContent/WEB-INF/jsp/menu.jsp

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

File:WebContent/WEB-INF/jsp/footer.jsp

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


File:WebContent/WEB-INF/jsp/contact.jsp

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


	







	
"/>

Note that in above JSP, we used tag to display the message from resource bundle.

One thing that we must note here is that in header.jsp file, we have specified two links to select language. The link sets a request parameter ?lang= when user click on this link. Note that spring identifies this request parameter by using LocaleChangeInterceptor interceptor and change the local accordingly. Also note that while configuring LocaleChangeInterceptor in spring-servlet.xml file, we have specified property “paramName” with value “lang”


Thus the Spring framework will look for a parameter called “lang” from request.

That’s All Folks

That’s pretty much it :) We just added Internationalization and Localization support to our demo Spring 3.0 MVC application. All you have to do is just execute the app in Eclipse. Press Alt + Shift + X, R.

contact-manager-screen-en

contact-manager-screen-de

Download Source Code

Click here to download Source Code (10.2kb)

Moving On

Today we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We used LocaleChangeInterceptor to intercept the change in locale and ReloadableResourceBundleMessageSource class to add message resources properties. In the next part we will discuss Themes in Spring MVC and how to implement it. I hope you liked this article. Feel free to post your queries and comments in comment section.



92 Comments

  • Semika 1 July, 2012, 0:59

    My application is running on tomcat7. But this loads only the ‘en_US’ property file. After clicking on ‘de’ link, it does not load the relevant property files. Please help me.

  • Lukas 3 July, 2012, 22:06

    Hello
    I have got problem i need to have got my properties file in external folder (not in war file)
    so what i need to do ?
    example my file example_en.properties is on /usr/local/websites/example/properties/example_en.properits ??
    I think that i must extends class : org.springframework.context.support.ReloadableResourceBundleMessages
    but i havent got any idea how to do it :(

  • Benni 11 July, 2012, 20:28

    Thx @Matthew Cachia for your answer:

    with

    we can fix the problem

  • Shiv Kumar Ganesh 24 July, 2012, 14:34

    Hi,
    Since you have a really great blog and I too found some of the most interesting starting points here itself, I would recommend you to also help people to solve the problem which they face while practicing out your tutorials. I dont see any reply to the above messages and I am also suffering with the same problem. Please suggest a fix for the above. I know its not a forum but some help would be appreciated.

  • dcelso 27 July, 2012, 0:46

    Hi, I solved it replacing in the web.xml this two beans:

    By this other subsection,

    An to do it, you need add xmlns:mvc to the beans section. My beans sections it this:

  • Camel 31 July, 2012, 1:19

    Hi
    I have same problem like many of you. After 2 hours found this link which help me.
    For all of you who are using mvc:annotaion-driven … try this
    http://stackoverflow.com/questions/8738680/how-to-change-multi-languages-with-spring-on-project-egovframework
    … or who don’t lile long texts like me … there is a code for application-context.xml

  • Michael 7 September, 2012, 22:33

    Hello everyone!
    Thank you, very good explanation!
    But I have one question, can you imagine that our site has a lot of articles and we want to have translation for two languages. I think, text of all articles storing in messages_de.properties and messages_en.properties it’s very bad idea. So my question is, how is it possible to handle it in Spring MVC?
    Thank you!

  • Krzysztof 13 September, 2012, 22:28

    Doesn’t work for me
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path ‘/WEB-INF/template/layout.jsp’.

  • Vadim Lotar 22 November, 2012, 20:09

    It’s brilliant article.
    Thanks a lot.
    I would like to add just one more thing in order to make it happens:

     
    <interceptors>
            <beans:ref bean="localeChangeInterceptor"/>
    </interceptors>
    

    Without this piece of code, it does not work for me.

  • Chandan Gawri 13 January, 2013, 14:11

    Hello every one.
    Even I got into same problem and tried every one’s approach. I am very new to spring framework.
    Matthew Cachia: I could not find where i can fit servlet-context.xml in this demonstration.
    Vadim Lotar: when i put this line into my spring-servlet.xml it just show error into configuration over tag.
    Help required.

  • Biro 13 February, 2013, 15:09

    Hy,
    thank you for the explanation
    I have a question : how can i put this message into a confirmation box ? or alert ?

  • John 1 March, 2013, 15:57

    Hi, I have encountered problem with mapping to the controller when using

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="interceptors">
    			<list>
    				<ref bean="localeChangeInterceptor"/>
    			</list>
    		</property>
    	</bean> 

    , since it can’t map to my controller with localjost:8080/Spot.

    However, if this bean is commented, my

     org.springframework.web.servlet.DispatcherServlet 

    forwards properly to the controller

    @Controller
    public class HelloWorldController {
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String handleRequest() {
            return "hello";
        }
    
    }
    
    • John 1 March, 2013, 16:07

      Ok, I guess I should read more carefully. I’va found a solution that adding

       <mvc:annotation-driven/> 

      to the dispatcher-servlet.xml fixes the problem :)

  • faiza 13 March, 2013, 14:16

    how can i use springs internationalisation from a javascript..?
    or is there a way to access .properties file contents from a javascript??
    please help

  • extarc 12 April, 2013, 16:27

    Thankyou, Its working fine.but my requirement is I have to load the values in two hashMap variables from database table. How can we configure in the dispatcher servlet. Please help me

  • Adam Wu 21 August, 2013, 12:09

    How can I specify the message_en.properties in a specific path?
    The folder structure is as below:
    Jboss
    |-message_en.properties
    |-webapps
    |-Spring3MVC.war

    • Adam Wu 21 August, 2013, 12:16

      Jboss
      “|-message_en.properties
      “|-webapps
      ““““|-Spring3MVC.war

  • JavaFreak 4 September, 2013, 17:35

    Changing the bean as below worked. Also add messages.properties file

    messages

    <!– –>

  • sagar 18 September, 2013, 20:40

    How can we do multiple form based application in spring3

  • massimo 3 October, 2013, 21:42

    hi ,
    I have my path esternal of web application and using Jboss as AS.
    In my *-servlet.xml i’m using a variable to define a folder for *propeties :

    how can i use your code :

    many thanks for your article.
    kings regard Massimo

  • Mamuka Arabuli 27 October, 2013, 10:23

    Hello everyone
    what do you think , if users cookies are disabled, will cookielocaleresolver do it’s work?

  • Mamuka Arabuli 28 October, 2013, 8:50

    nice tutorial
    what is better way to use cookielocaleresolver or sessionlocaleresolver?
    cookies may be turned off…

  • vikas yadav 7 November, 2013, 14:59

    No message found under code ‘label.title’ for locale ‘en’

    ths is the error message
    can anyone tell whats going wrong

    • Boechi 6 June, 2014, 17:17

      Probably you miss typed the label.title in the properties file (or in the jsp file).

  • Niklas Rtz 15 December, 2013, 17:03

    Thanks! This tutorial works right away for my setup: IntelliJ + Spring.

  • K. Amin 8 January, 2014, 22:44

    Thanks Viral. All your tutorials works like a charm without any issue. Very helpful…….

  • Sandip 9 April, 2014, 16:23

    Hi,
    I want to access sprinig messages in javascript how can i do this?
    pls provide tutorial.
    Thanks

  • Boechi 6 June, 2014, 17:22

    The DefaultAnnotationHandlerMapping is depricated.
    Solution for this is the following:

    <mvc:annotation-driven/>
    <mvc:interceptors>
    		<ref bean=&quot;localChangeInterceptor&quot;/>
    </mvc:interceptors>
    
  • Boechi 6 June, 2014, 17:23

    The DefaultAnnotationHandlerMapping is depricated.
    Solution for this is the following:

  • MM 17 June, 2014, 12:29

    Just a small hint: The german translation of “Add contacts” is wrong ;)
    You translated it with an “mathematical addition” which doesn’t make sense here. Instead “Kontakt hinzufügen” is the appropriate translation and used in most of the applications out there :)

    • Viral Patel 17 June, 2014, 13:34

      Thanks for that MM :) I will update the post.

  • Yuva Kumar R 8 July, 2014, 12:41

    Hi Viral

    Thanks for your nice tutorial, but am struck with this example…am getting “No message found under code ‘label.title’ for locale ‘en’”. can you tell whats going wrong, I have the resource folder with name “resources” under src folder as suggested by you. following is the file path…

    Spring3MVC1\src\resources\messages_en.properties.

    The content of the messages_en.properties is as follows :

    label.firstname=First Name
    label.lastname=Last Name
    label.email=Email
    label.telephone=Telephone
    label.addcontact=Add Contact

    label.menu=Menu
    label.title=Contact Manager

    label.footer=© test.net

    Thanks for your valuable articles.

    Yuva

  • quest 11 December, 2014, 19:38

    Very nice example. works fine

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]