Spring MVC i18n Web Application

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

I already created messageSource and made all my jsp pages search for messages using this messageSource. Currently, the language changes depending on the locale of the user browser.

So now I want to do it manually.

I found that the SessionLocaleResolver class can help, but I don’t know how to configure it in the application context file (which name is myAppName-servlet.xml).

I defined a bean:

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

But in which bean should I plug this in? Also, how to set a cookie associated with a locale in a user session?

+3
1

, , , , :

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

, xml 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>

:

siteLanguage=locale

.

: http://localhost:8080/SBrowser/deliveries.html?siteLanguage=fr enter code here

+6

Source: https://habr.com/ru/post/1706050/


All Articles