Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Locale change doesn't work

In my application I have defined the below beans but when I try to change the Locale using the parameter ex: ?lang=es It wont work for me and the thing shows out is the default locale defined in CookieLocaleResolver which is English.

here are my bean classes :

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="resources/i18n/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</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="cookieName" value="lang" />
    <property name="defaultLocale" value="en" />
</bean>

<bean
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor" />
        </list>
    </property>
</bean>

what I want to achieve is to change a language using parameter and save that inside the cookie related to that so for further request the locale is the new Locale language !

I also don't want to use the below class because It's deprecated already :

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

what is wrong with my declarative bean implementation ?

like image 635
M2hp Avatar asked Mar 13 '14 19:03

M2hp


People also ask

How do I use localeresolver in springspring?

Spring provides 4 concrete implementations of LocaleResolver as below: SessionLocaleResolver: use the locale attribute in the user session, with a fallback to the specified default locale or the request’s accept-header locale. CookieLocaleResolver : persists a custom locale and/or a time zone information as the browser cookie.

How do I enable internationalization and localization support for Spring MVC?

Let's go through the basic steps for equipping a Spring MVC web application with internationalization and localization support. First, we will go through basic configuration to bring up a Spring MVC web application. We will then add the necessary resources for i18n and l10n support.

What is localization in Spring Framework?

Localization is the process of adapting internationalized software for a specific region or language by adding locale-specific components and translating text [ Wiki ]. Spring framework is shipped with LocaleResolver to support the internationalization and thus localization as well.

Can I customize the locale resolver used by The DispatcherServlet?

The fix for #25209 appears to have removed or at least limited the ability to customize the locale resolver that is used by the DispatcherServlet. The introduction of a default LocaleResolver bean means that Spring Boot's auto-configuration of a LocaleResolver backs off as it is presumed that the bean that's now present is one provided by the user.


1 Answers

I believe you should define interceptor like this:

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

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptor>
</mvc:interceptors>

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

MVC namespace is defined like this:

xmlns:mvc="http://www.springframework.org/schema/mvc"
....
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"

Make sure you use proper xsd version.

like image 73
kamil Avatar answered Oct 13 '22 06:10

kamil