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

	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basename" value="classpath:messages" />
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>

	<bean id="localeChangeInterceptor"
		class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
		<property name="paramName" value="lang" />
	</bean>

	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
		<property name="defaultLocale" value="en"/>
	</bean>

	<bean id="handlerMapping"
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors">
			<ref bean="localeChangeInterceptor" />
		</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 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"%>

<h3><spring:message code="label.title"/></h3>

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

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

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

<p><spring:message code="label.menu"/></p>

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

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

<spring:message code="label.footer"/>

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"%>
<html>
<head>
	<title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>



<form:form method="post" action="addContact.html">

	<table>
	<tr>
		<td><form:label path="firstname"><spring:message code="label.firstname"/></form:label></td>
		<td><form:input path="firstname" /></td> 
	</tr>
	<tr>
		<td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td>
		<td><form:input path="lastname" /></td>
	</tr>
	<tr>
		<td><form:label path="lastname"><spring:message code="label.email"/></form:label></td>
		<td><form:input path="email" /></td>
	</tr>
	<tr>
		<td><form:label path="lastname"><spring:message code="label.telephone"/></form:label></td>
		<td><form:input path="telephone" /></td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="submit" value="<spring:message code="label.addcontact"/>"/>
		</td>
	</tr>
</table>	
	
</form:form>
</body>
</html>

Note that in above JSP, we used <spring:message> 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”

<property name="paramName" 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.



91 Comments

  • mateusz f 16 July, 2010, 21:41

    I have a queston. You defined two languages, and de. What if a visitor comes who is from let’s say Canada. Which locale will be chosen for him as default? Is there a way to specify a default locale at all?

    • Viral Patel 26 July, 2010, 15:08

      @mateusz f. nice question. There is a way in spring to define default local. This can be done by passing parameter defaultLocale to CookieLocaleResolver bean.

      <bean id="localeResolver"
      	class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
      	<property name="defaultLocale" value="en"/>
      </bean>
      
  • Jim 17 July, 2010, 15:47

    Thank you for your blog, much appreciated.

  • Jim 17 July, 2010, 15:49

    Thanks very much for the sample.

    Only question so far is that when I run mvn tomcat:redeploy it looks for mvc-basic-1.0.0-SNAPSHOT.war rather than mvc-basic.war that the install actually builds

  • Gerlad 22 July, 2010, 1:44

    Please SWF tutorial! Great Job

  • Ajit 26 July, 2010, 8:02

    gr8 job done
    Can you show security cache in spring 3

  • Luponiano 27 July, 2010, 13:33

    Hi Viral, nice tutorial.
    I am experiencing problems. I have followed your example and used two languajes, spanish and english. If I set the defaultLocale to ‘en’ all the page is visible on english no matter the spanish accept of the navigator. It is as if it setted how the page should be viewed as default unless the user specifies something different. Do you know if it can be used so the page is viewed in spanish if the navigator is set to spanish and to english otherwise?
    Setting the defaultLocale solved the problem of other navigators with another languaje setted.

    Thank

  • Luponiano 27 July, 2010, 19:27

    Hi Viral, I finally managed to solve the problem by extending the SessionLocaleResolver, overriding the determineDefaultLocale and injecting the supported languajes to the new resolver. It may sound a little complicated but it was not too much and now works like a charm.

    Thank you for the basis.

    • L Lobo 6 April, 2011, 3:03

      Hi Can you please post your code…
      Thanks

  • saravanan 10 August, 2010, 19:34

    it is very nice website for spring, it is very useful to me.

  • Bharath 12 August, 2010, 11:29

    Thanks for your tutorial…. its really helpful…..

  • Kiran Hegde 16 August, 2010, 16:58

    Hi Viral, good work man…
    Latest tech stuff…

  • Alex 30 September, 2010, 3:48

    Great tutorial, finally I’m making some stuff with spring, thanks man.

  • damiencharp 3 October, 2010, 22:49

    Thanks ! Keep up the good work.

  • amila 6 October, 2010, 14:40

    place can u give another tutorial in netbeans

  • amila 6 October, 2010, 14:43

    if u can give me same ref:
    thank u

  • Gal Levisnky 21 October, 2010, 2:07

    Hi,
    Very nice introduction, however it is possible to make it even with less config:
    remove the handlerMapping from the xml, and replace the LocaleChangeInterceptor with the following:

    than in the header user ?locale=de instead of ?lang=de.

    Thanks.

  • sai 28 October, 2010, 3:58

    hi Luponiano

    can u post ur session local resolver example?

    thanks much

  • Hriday 28 October, 2010, 23:14

    Hi,
    Thanks very much … it was really helpful. But I stuck in problem. In case I already have a parameter in my url in tht case ?lang=en breaks
    eg. I hv a url
    http://hostname/path/index.html?id=1
    Now if click on lang English, it will append the ?lang=en to it which is creating problem. Can you suggest some solution to this.

    Thanks Again
    Hriday

  • VK 31 October, 2010, 22:21

    Nice work man.. really appreciate this… helped me to quickly learn the basics of spring.

  • Falkster 4 December, 2010, 23:41

    Great stuff. Thanks a lot!

    PS:

    There are two little mistakes in the German translations. It’s „Kontakt-Manager“ not „Kontakt Manager“, the hyphen is obligatory on compounds (you could also translate it fully into german and write it as one word: „Kontaktverwaltung“). And you can’t say/write „Addieren Kontakt“. „Kontakt addieren“ would be O.K., but „addieren“ is commonly related to a mathematical context, so „Kontakt hinzufügen“ would be much better.

  • Hitesh Joshi 22 December, 2010, 13:01

    Nice Tutorial Viral. Have a question though related to the following:
    “”

    In the above, the messages.properties and related files need to be present in the classpath of the application. Can I give a relative path with respect to the classpath? Basically, my problem is that I have all my messages.properties related files in the JBoss Server config location and not in the created WAR file. So, how should I configure the messageSource Bean.

    Thanks!

  • Diman 28 January, 2011, 6:47

    Hi Viral,
    thanks a lot for example. Localisation in the context of your tutorials means, user has to tell us where he comes from(or what language he/she speaks), right? Is there any way in spring, to check where he comes from… also to be able to give user a suggestion of his language. Example, if user accessing my webpage from chinese (or russian) server… Maybe I can already display contact form in chinese (or in russian).

    Thanks and Best Regards,
    Diman

  • suma 25 February, 2011, 12:01

    Hi,

    I have implemented localization the same way you have described.
    I am getting an exception
    ERROR 2011-02-25 12:27:01,448 [http-8080-1] org.springframework.web.servlet.tags
    .MessageTag: No message found under code ‘title.xxx’ for locale ‘en_US’.
    javax.servlet.jsp.JspTagException: No message found under code ‘title.xxx
    for locale ‘en_US’.

    Any ideas

    • Viral Patel 25 February, 2011, 14:54

      Hi Suma, Check your JSPs and see if you are using any message key “title.xxx”? I think there is a mismatch in the values defined in message resource properties and the JSPs.

  • ricky 27 February, 2011, 8:17

    Hi Suma, I had the same issue. Just make sure you create the ‘resources’ folder as a ‘Source Folder’ as Viral indicated. If you create the ‘resources’ folder as a ‘Folder’ the 2 messages property files does not get copy to your war file.

    • Ruchi 15 October, 2013, 14:55

      ricky
      Thanks for your suggestion. I was getting the same issue which suma was getting and your suggestion has resolved my issue. But please let me know what is the difference between folder and source folder.
      Thanks

  • Sandeep Tyagi 28 February, 2011, 10:49

    Very nice, and precisely build example. keep it up guys.

  • Borpf 4 May, 2011, 20:51

    hmm.. ok this tutorial can be used for internationalisation, but it just describes a way to replace some text by submitting a request parameter. Real locale switching would matter in different outputs e.g. on DateFormats like en=01/31/1970 de=31.01.1970 or currency outputs.. How about extending this example to show, that switching locale by using LocaleChangeInterceptor and LocaleResolver would also effect these points?

  • Goutham 25 May, 2011, 11:17

    Hi , nice tutorial,However i have a prob here. Clicking on the en|de i am not getting tge diff locale..
    I have added the following in my own xml:

    And my messages.properties are in place for both the languages.
    And my header has the following :
    en |
    de |

  • Goutham 25 May, 2011, 11:50

    My ?lang=de as well as ?lang=en is displaying the same text even though text in both the properties is different. I am not able to change my local. I ve added this my XML :

  • Sara 5 July, 2011, 18:57

    In case you’re using Spring 3.0.5, please be aware of the conflict between and DefaultAnnotationHandlerMapping (http://forum.springsource.org/showthread.php?81238-Conflict-between-lt-mvc-annotation-driven-gt-and-DefaultAnnotationHandlerMapping).
    I.e.instead of defining the interceptor by creating, as in the example, the bean localeChangeInterceptor and then putting it in bean handlerMapping, you would just need to define it inside the tag, like this:

    • Kiran 12 August, 2011, 3:26

      Hi I am getting error saying
      No context loader listner found

  • Bolat 12 August, 2011, 1:52

    Dear Viral,

    Unfortunately, the necessary amended spring-servlet.xml definition for Spring 3.0.5 by Sara (last comment) did not come out (as it appears that xml tags are automatically omitted when posting). Would you happen to know how to define it inside the tag as there is a conflict when using the standard spring-servlet.xml file for Spring 3.0.5, which includes the xmlns:mvc=”http://www.springframework.org/schema/mvc tag in the header, which thereby enforces the declaration of the mvc:annotation-driven tag.

    Many thanks.

    Kind regards,
    Bolat

  • Bolat 12 August, 2011, 2:18

    Dear Viral,

    Just to be clear regarding my post a few minutes ago, if I remove the xmlns:mvc=”http://www.springframework.org/schema/mvc tag in the header and also the mvc:annotation-driven tag, the language switching from English to German works very well, but I prefer to have these tags as they recommended in the latest release of Spring, which is 3.0.5. The strange thing is, that with these tags, it reads the messages_en.properties file and naturally displays the form in English, but does not do the switching to German upon clicking ‘de’, it remains as English.

    Any help or directions will be most appreciated.

    Many thanks,
    Bolat

  • Bolat 12 August, 2011, 22:16

    Dear Viral,

    I have one Final question and would appreciate a reply: I noticed that you have put HTML decimal ü to display the dotted u. Is there way of doing this directly by Typing the actual letter in the editor instead of giving numerical codes, especial for the dotless i (code ı) as when I put the dotless i directly (via Notepad using Windows 7 language for Tukish, mapped to keyboard key I), upon running the application, it comes out as ‘?’.

    Many thanks.

    Kind regards,
    Bolat

  • Bolat 13 August, 2011, 3:55

    REVISED: Dear Viral,

    I have one Final & quick question and would really appreciate a reply: I noticed that you have put the html decimal code 252 to display the dotted u / ü. Is there way of doing this directly without Typing the the codes, but rather type the actual letters in the Editor, especial for the Turkish dotless i / ı letter (html decimal code 305) as when I put the dotless i directly via an Editor using Windows 7 language for Turkish (mapped to keyboard key letter i), it comes out as ‘?’ when running this tutorial on the browser. This is IMPORTANT as I may have lots of translations to do and to put html decimal codes for each special character opens up room for potential errors and of course it is very time consuming. Any guidance on this matter will be much appreciated.

    Many thanks.

    Kind regards,
    Bolat

    • Vandana 5 November, 2011, 11:06

      Hello Bolat, Since you have faced the same reply as me, and the solution is not clear from Sara’s or our post, can you please share the same. Problem is the same: using stops the locale change functionality. And I do need to use the mvc annotation, fr the Ajax to work.

  • CAD 8 September, 2011, 5:05

    Hi,
    Great article, i have a question, Anyone know if can i access to messages with an expression like $message['code'] or something similar instead use spring:message tag?

    Regards.
    CAD

  • rojanu 6 November, 2011, 19:13

    @Bolat properties files can only be in ASCII therefore any character thats is not ASCII must presented in \u notation. native2ascii, which comes with java, used to convert the file e.g. “native2ascii -encoding utf-8 input.txt messages_tr.properties”

  • ram 14 November, 2011, 16:56

    Its really very useful. I really appreciate it. Thanks folks.

  • Kamil 20 November, 2011, 18:25

    Thanks, very useful. Do you have idea how to setup language switch in url like
    http://domain/en/…. (english)
    http://domain/… (english)
    http://fomain/de/… (german)
    etc.
    so that website in english can be indexed by google bots

  • isha 3 January, 2012, 15:43

    I’m not getting different language when I’m clicking on the link “de”.
    Even When I’m using by default other than english same ,english language is only coming…
    Can anyone help me to know what misake I’m doing…….????

  • laff81 13 January, 2012, 15:59

    Doesn’t work?

    Try:

    mvc:interceptors
    bean id=”localeChangeInterceptor”
    class=”org.springframework.web.servlet.i18n.LocaleChangeInterceptor”
    property name=”paramName” value=”lang” /
    /bean
    /mvc:interceptors

    And remove the bean “handlerMapping”.

  • Kam 17 January, 2012, 17:53

    Excellent tutorial. If you are working in IDE and not bothered about deployment, make sure that you declare your resource folder as Source folder so that the messages*.properties files are automatically deployed in /WEB-INF/classes folder.

  • Krzysztof Blaszke 18 January, 2012, 18:12

    Hi! Thak You for great tutorial! :-)
    I had to add:

    to my servlet-context.xml to applikaction work.
    Best regards

    K.B.

  • Kelvin 24 January, 2012, 4:23

    javax.servlet.jsp.JspTagException: No message found under code ‘label.title’ for locale ‘en’.

  • Tullio 25 January, 2012, 21:22

    After different experiments I defined my messageSource bean like this and it worked:

    /WEB-INF/text/messages

  • Tullio 25 January, 2012, 21:23

    After different experiments I defined my messageSource bean like this and it worked:
    beans:bean id=”messageSource” class=”org.springframework.context.support.ReloadableResourceBundleMessageSource”
    beans:property name=”basenames”
    beans:list
    beans:value>/WEB-INF/text/messages/beans:value
    /beans:list
    /beans:property
    beans:property name=”defaultEncoding” value=”ISO-8859-1″ /
    beans:property name=”cacheSeconds” value=”1″ /
    /beans:bean

    Sorry about the format…

  • Tharindu 3 February, 2012, 20:04

    How to stop it from going to RequestMapped method. Because I tried, and it was taken by the Controller method when I tried to change the language. Although lang request parameter is there, as it is coming from a URL which mapped to a RequestMapping in Controller class, it does not give me the intended result. Am I missing a piece of puzzle here? :(

  • Dorley Pan 15 February, 2012, 8:38

    I added chinese characters to the example (messages_zh.properties), but my chinese characters are not displayed properly (instead of actual characters there were shown as ?).

    I tested by reverting to not using tiles2 and it will display the chinese characters correctly. How can chinese characters works with tiles2 ?

  • lakshmi 22 February, 2012, 13:43

    i need code without using those links en & de. Because in system itself we can select lang.. which we want.

  • Kandhi 23 March, 2012, 14:50

    Hi
    I need the locale checking in js , ie I need to check the locale in document on ready for setting the message .But I couldn’t get the locale in js .
    I used above method and my jsp is:

    English
    |
    Arabic

    this is working fine .

    But in that pages a message came from document onload function , so i need to check the code

  • Japan Trivedi 23 April, 2012, 15:52

    Hi,

    Thanks for the nice tutorial. But I have a problem while doing the same configuration using spring framework 3.1 without any xml.

    I have created one application that uses no xml files for the web application using Spring Framework 3.1. I have done the above mentioned configuration for Internationalization in a java file that works exactly same way spring-servlet.xml works.

    But I am not able to successfully implement internationalization.

    Have you tried this??

    Do you have any solution in this context.

    Thanks.

  • Larry 18 May, 2012, 3:08

    Thanks for the tutorial – having an issue though (XML configuration is obviously not my string suit, and frankly driving me a little nuts).

    I’ve placed my messages_en_US.properties file in ./src/main/webapp/resources/international, but haven’t found a way to resolve the location. Every time it runs, it’s throwing an error “Can’t find bundle for base name messages, locale en_US”, no matter how I prefix the location in the bean. I would have thought the mapping for /resources/ would have handled this, but if so, I’m stuck.

    Secondary issue is that the “defaultEncoding” property name in messageSource bean fails with “No setter found for property ‘defaultEncoding'”. Less critical, but would like to know what’s up with that, too.

    Running STS 2.9.1.RELEASE and using SpringFramework 3.1.0.RELEASE.

    Hoping one of the brilliant developers who read this can help. Here’s my WEB-INF/spring/appServlet/servlet-context.xml file:

    <!– –>

    • Nhat Dinh Van 3 June, 2012, 16:37

      Yeah! final i can do it.

  • Amit 29 May, 2012, 18:31

    Thanks for the tutorial …

    I’m using Spring 3.1.0 and configured in the same way but it is always taking value from default locale

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]

Current ye@r *