Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3 Locale changing using a link not working

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.

like image 338
Firdous Amir Avatar asked May 25 '11 18:05

Firdous Amir


3 Answers

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

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>
like image 187
sonida Avatar answered Oct 19 '22 09:10

sonida


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>
like image 23
Pau Giner Avatar answered Oct 19 '22 11:10

Pau Giner


Another thing that can help others:

In my case, I MUST add in the applicationContext.xml. Putting it in the spring-servlet (ref. dispatcher), not worked at all.

like image 30
Felipe Volpato Avatar answered Oct 19 '22 11:10

Felipe Volpato