Spring by Example - Spring Web MVC Locale Change

Skip to main content.

Spring Web MVC Locale Change

Overview

Simple example showing one way to setup internationalization. The locale can be changed by passing in a request parameter. To change the locale to french, just add the parameter 'siteLang=fr' to the URL for your site.

Spring XML Configuration

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <value>messages</value>
    </property>
</bean>

<!-- Declare the Interceptor -->
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="siteLang"/>
</bean>

<!-- Declare the Resolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

Message Property Files

The message resources for this configuration should be located in the root of the classpath. The messageSource bean was configured to look for resources starting with 'messages' and will look for a locale specific resource before using the default message resource.

/messages.properties

welcome.title=Welcome to LCM, the Lawn Care Management system

/messages_fr.properties

welcome.title=Bienvenu &agrave; LCM, le syst&egrave;me de gestion de pelouses.

JSP

The JSP uses the JSTL format tags to display the internatilized message.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title><fmt:message key="welcome.title"/> </title>
</head>
<body>
    <p><fmt:message key="welcome.title"/> </p>
</body>
</html>

Comments

 

SpringMetaForm
Title Spring Web MVC Locale Change
Description Simple example showing one way to setup internationalization.
OpenSourceLicenseList Apache License, 2.0
SpringCoreVersionList 2.0
SpringCoreList Spring Web MVC
SpringProjectList

OpenSourceProjectList