CodeCannibal
CodeCannibal

Reputation: 344

Spring MVC localization in controller class is not working

I created a Spring MVC project and tried to change to session locale within an controller class.

So, here are the most important files from my project:

applicationContext.xml (only partly..):

  <context:component-scan base-package="de.my.packages.controller" />

  <context:annotation-config />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/springMessages" />
    </bean>

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="de" />
</bean>

JSP file ( only partly..)

<spring:message code="test.abc" />

springMessages.properties

test.abc=This is a testText.

springMessages_de.properties

test.abc=Das ist ein TestText.

Controller-class

@Controller
public class InitController {

    @Autowired
    private SessionLocaleResolver localeResolver;

    @RequestMapping("/testPage")
    public String showLogin( HttpSession session, HttpServletRequest request, HttpServletResponse response){
        localeResolver.setLocale(request, response, Locale.ENGLISH);
        return ("testPage");
    }
}

So - when I call

http://localhost:8080/myContext/testPage

the JSP will be called and displayed. But although I changed the language in the controller to English , the displayed text is German. I did not add a localChangeInterceptor class, because my target is not to change the language via URL.

Any idea how to fix that?

Upvotes: 1

Views: 844

Answers (1)

Roman C
Roman C

Reputation: 1

springMessages.properties is the default message resource bundle. To use English resource bundle you should copy this file to springMessages_en.properties. Because you set locale with

localeResolver.setLocale(request, response, Locale.ENGLISH);

the resource bundle is used springMessages_en.properties.

Upvotes: 1

Related Questions