Setting up locale switching with Spring MVC

First of all, I must say that I am an absolute newbie to Spring Application development. I am trying to switch the language from 'en' to 'de'. For this I found the config below which I entered into my mvc-dispatcher-servlet.xml

 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="Messages" />
   </bean>

   <!-- Localization Start -->
   <bean id="localeResolver"
         class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
          <property name="defaultLocale" value="en" />
   </bean>

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

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

      

After that I expect to be able to change the locale by adding '? language = de 'for the existing URL. So the request http: // localhost: 8080 /? Language = de 'should switch the locale. It didn't work. The website is displayed in a specific language by default

My properties files are in / src / main / resources. Names: "Messages_en.propperties" and "Messages_de.propperties". If I switch the default language to "de", the correct language file is loaded and the website is displayed in German. Does anyone know what is wrong with my configuration?

+3


source to share


1 answer


I believe you need to register LocaleChangeInterceptor with interceptor in Spring

<!-- Declare the Interceptor -->
<mvc:interceptors>    
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
          p:paramName="locale" />
</mvc:interceptors>

      



LocaleChangeInterceptor is configured to look up the parameter name 'locale' to indicate a change to the user's locale and is registered as an interceptor using the Spring MVC namespace. For example, adding "locale = es" to the URL will change the locale to Spanish.

+2


source







All Articles