Spring MVC 3 Locale changing using a link not working

Firdous Amir picture Firdous Amir · May 25, 2011 · Viewed 15.6k times · Source

Edit: My Spring framework version 3.0.5

A small issue here, The language is not changing when I click the language changer link.

The language files (messages_xx.properties) are in the classpath i18n directory. The files are:

i18n/messages_en.properties
i18n/messages_ar.properties

Spring Configuration

<!-- Component scanner. This is used to automatically find Spring annotations like @Service and @Repository -->
    <context:component-scan base-package="com.keype" />

    <!-- Annotation driven programming model -->
    <mvc:annotation-driven />   
    <context:annotation-config />
    <mvc:resources mapping="/static/**" location="/static/" />


    <!-- Session Object Configuration -->
    <bean id="session" class="com.keype.system.Session" scope="session">
        <aop:scoped-proxy />
    </bean>

    <!-- The View Resolver -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"
          />

    <!-- i18n Configuration. Default language is english. Change language using ?language=en -->
    <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.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <!-- Message text files. This is set UTF-8 to display Arabic UTF correctly. -->    
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:i18n/messages" />
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

A section from the JSP Code

<a href="?lang=ar"><spring:message code="header.arabic" /></a> | 
    <a href="?lang=en"><spring:message code="header.english" /></a> 

The issue is, when I click the above link to change the language, the locale changing functionality is not working. I tested by changing the "defaultLocate" to "ar" and I'm getting Arabic text.

What could possibly be wrong here? There is nothing in the tomcat log also.

Answer

Pau Giner picture Pau Giner · May 25, 2011

You have to register the localeChangeInterceptor among the MVC interceptors for Spring-MVC to consider it. Add the interceptor to the configuration:

<mvc:interceptors>  
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
              <property name="paramName" value="lang"></property>
        </bean>
</mvc:interceptors>