Search code examples
springjsplocale

Spring Framework Localization always reads from en (English) file


I have followed the tutorial below to set up localization for my web app.

http://viralpatel.net/blogs/spring-3-mvc-internationalization-i18n-localization-tutorial-example/

I have three properties files: messages_en_properties, messages_es.properties, and messages_zh_CN.properties

Each file has the following line:

loging.name=(Username translated to the corresponding language)

In my .jsp file I set my table data to display the username using the following line:

<td><spring:message code = "login.name" text = "Default Text"/></td>

This works, but it only will ever read from my English properties file. I have tried setting the locale manually in the .jsp file to zh_CN and es but I still receive the English file values. I have even tried setting the locale to a country for a file I do not have and it still displays the text from the English file, and not the default text. I am positive the locale is actually being changed since the browser will ask if I would like the page translated.

Here is the part of my code that sets up the localeChangeInterceptor.

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

<beans:bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <beans:property name="interceptors">
        <beans:ref bean="localeChangeInterceptor" />
    </beans:property>
</beans:bean>

Also tried changing to the following...

<mvc:interceptors>
    <beans:bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

<beans:bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="en" />
</beans:bean>

<beans:bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</beans:bean>

Any help would be greatly appreciated.


Solution

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

    as

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