Locales in Spring MVC


Locales in Spring MVC

In this section, you will learn about Locales in Spring MVC.

In this section, you will learn about Locales in Spring MVC.

Locales in Spring MVC

In this section, you will learn about Locales in Spring MVC.

Set of parameters that determines the language, country and any preference of the user in a user interface is known as a locale.

Spring MVC also support internationalization and localization. In Spring MVC,  DispatcherServlet allows you to automatically resolve messages by using client's locale. This can be done using  LocaleResolver objects.

When a request came, DispatcherServlet searches for a locale resolver. You can get the locale resolved by the locale resolver using the method RequestContext.getLocale().

For changing the locale under particular circumstances, you can also add interceptor to the handler mapping. For example, based on some request parameter, you can change locale.

Given below the Local resolvers in the Spring :


The client' s request header accept-language is read by this locale resolver to resolve the locale. Generally, client's operating system locale is held by this header field.


This locale resolver inspects the cookies for finding the locale. If a local is specified in the cookie, it uses this locale.

Also, using the property of the locale resolver, you can set the name of the cookie in addition to the maximum age. You can define CookieLocaleResolver as follows :

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
	<property name="cookieName" value="locallang"/>
	<property name="cookieMaxAge" value="100000">

Properties of CookieLocaleResolver

The description of the properties of CookieLocaleResolver is given below :

Property Name Default Value Description
cookieName classname + LOCALE This property is used to set the cookie name.
cookieMaxAge Integer.MAX_INT This property's value is in seconds. It is the maximum time a cookie will live on the client system.
cookiePath / This property is used to confined the visibility to specific parts of your site. When the cookiePath is set, it will be visible to that path and the path below it.


This local resolver is used to get the locales from the session which might be related to the request from the user.


The below example will show you that calls to all *.view resources containing a parameter named UserLang will now change the locale. For example, the following request - http://www.roseindia.net/index.view?UserLang=de will change the language to German.

<bean id="localeChangeInterceptor"
	<property name="paramName" value="UserLang"/>

<bean id="localeResolver"

<bean id="urlMapping"
	<property name="interceptors">
			<ref bean="localeChangeInterceptor"/>
	<property name="mappings">