Christian
Christian

Reputation: 33

Configuring locale switching with Spring MVC

First of all I have to say, that I am an absolute beginner in developing Spring Application. What I try to do is to switch the locale from 'en' to 'de'. For this I found the configuration below witch I put in 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 that I can change the locale by adding '?language=de' behind a existing URL. So the request 'http://localhost:8080/?language=de' should switch the locale. This didn’t work. The website is shown in the defined default language

My property files are located in /src/main/resources. The names are “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 shown in german. Has someone an idea what’s wrong in my configuration?

Upvotes: 3

Views: 1244

Answers (1)

techPackets
techPackets

Reputation: 4516

I believe you have to register the LocaleChangeInterceptor with an interceptor in Spring

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

The LocaleChangeInterceptor is configured to look for the parameter name 'locale' to indicate a change of the user's locale, and is registered as an interceptor using the Spring MVC Namespace. For example, adding 'locale=es' to a URL would change the locale to Spanish.

Upvotes: 2

Related Questions