Reputation: 11
I have set up i18n to my Spring MVC project . The language is not changing when I click on the link:
<a href="?lang=hi">Hindi</a>
<a href="?lang=en">English</a>
controller-servlet.xml
<mvc:annotation-driven/>
<context:component-scan base-package="com.avvas.search.controller" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:resources/messages" />
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="0" />
</bean>
<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.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/javascript/**" location="/javascript/" />
local changing links:
<a href="?lang=hi">Hindi</a>
<a href="?lang=en">English</a>
The issue is that when i click on hindi the page should be displayed in Hindi language, Language is not getting changed. However i changed default language to hindi <property name="defaultLocale" value="hi"/>
then it is taking local as hindi but when i click on english local change link the local is not getting changed.
Upvotes: 0
Views: 1517
Reputation: 151
Here is the Complete code. Basically we need to register our interceptors explicitly.
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
In above given xml you have given "localeChangeInterceptor" but you haven't mentioned anywhere that it should intercept all the requests by default.
So Spring will load all the beans but it wouldn't register it as interceptor until or unless you define that which requests it should intercept.
In below example i am defining it in mvc interceptor and telling the framework that it should consult that interceptor before processing any request.
<mvc:interceptor>
<mvc:mapping path="/**/**/" />
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
Upvotes: 0
Reputation: 18214
<mvc:annotation-driven/>
is registering its own handler mapping (RequestMappingHandlerMapping
) and your interceptor configuration is not applied to that.
Define your interceptor like this:
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
Upvotes: 2