Home Tutorial Spring Spring Internationalization


Share on Google+Share on Google+
Spring Internationalization
Posted on: May 21, 2012 at 12:00 AM
In this tutorial you will learn how to localize our aplication

Spring Internationalization

To write an application that supports multiples languages, you need to add the following code in your dispatcher.

1. At first message resources

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<property name="basename" value="/WEB-INF/locale/message" />
	<property name="defaultEncoding" value="UTF-8" />

2. Add the LocaleChangeInterceptor as

	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
	p:paramName="locale" />

3. add the SessionLocaleResolver

<bean id="localeResolver"
	<property name="defaultLocale" value="en_ES" />

4. change the character encoding of your page

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
and include the message tag as
<%@ taglib prefix="message" uri="http://www.springframework.org/tags"%>

5. Replace all labels as

<message:message code="student.roll.no" />

A typical JSP page will look as follows

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="message" uri="http://www.springframework.org/tags"%>
<title>Please Change Language Here</title>
<script type="text/javascript">
function changeLanguage() {
var lang = document.getElementById("locale");
self.location = "load-form?locale=" + lang.value;
<br />
<br />
<table align="center" cellpadding="5" cellspacing="5">
<td><form:form action="process-form" commandName="sampleForm"
method="POST" enctype="multipart/form-data">
<table align="center">
<td><message:message code="student.roll.no" /></td>
<td><form:input path="rollNo" /></td>
<td><message:message code="student.name" /></td>
<td><form:input path="name" /></td>
<td><message:message code="student.address" /></td>
<td><form:input path="address" /></td>
<td align="center" colspan="2"><input type="submit"
value="Process" /></td>

<form:form action="load-form" method="get" commandName="languageForm">
<td><form:select path="locale" onchange="changeLanguage()">
<form:options items="${languages}" itemLabel="langName"
itemValue="value" />

When you run this application it will display message as shown below:

Download Source Code


Related Tags for Spring Internationalization:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 21, 2012

Recommend the tutorial

Advertisements Advertisements