Madhesh Madhesh - 1 month ago 9
Java Question

spring3 internationalization not working properly

I am newbie to spring.I used spring internationalization in my project.Below is my configuration

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/properties/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

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

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


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


In the properties folder i have three files.
messages.properties

label.username=user


messages_en.properties

label.username=hddhd


messages_es.properties

label.username=lalal


i display the message using
<spring:message code="label.username" />
this tag.

But this one always prints the value from messages.properties.
It always prints user.

even i give the url like below

[http://localhost:8080/Project/?lang=en
http://localhost:8080/Project/?lang=es][1]


why values not taken from the files messages_en and messages_es

Any help will be greatly appreciated!!!

Answer

When using <mvc:annotation-driven /> configuring the DefaultAnnotationHandlerMapping will not work. You should use the <mvc:interceptors /> element to register the interceptors.

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

Next to that it doesn't work in newer versions of Spring (everything after 2.5) doesn't use the DefaultAnnotationHandlerMapping anymore in case of <mvc:annotation-driven />.

Comments