views:

3276

answers:

1

In a web application written using spring-MVC, I want to allow users to change the current language by clicking on a link which text is the name of the language.

I have already set up a messageSource and made all my jsp pages find the messages using this messageSource. Currently, the language is changing depending on the locale of the user browser.

So, what I want to do now is to allow to change the locale manually.

I have found that the class SessionLocaleResolver could help, but I do not know how to set it up in my application context file (which name is myAppName-servlet.xml) .

I have defined the bean :

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>

But in which bean should I plug this ? Furthermore, how do I set a cookie related to locale into an user session ?

+1  A: 

All informations I needed were in the documentation, in front of me, at :

http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-localeresolver

In brief, I adapted the following xml to myAppName-servlet.xml

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

<bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

<bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor"/>
        </list>
    </property>
    <property name="mappings">
        <value>/**/*.view=someController</value>
    </property>
</bean>

And now, it suffices to access any page with the parameter :

siteLanguage=locale

to change the locale for the whole site.

For example : http://localhost:8080/SBrowser/deliveries.html?siteLanguage=frenter code here

madewulf