Reputation: 65
I am a new Spring developer tried to develop sample web app with two languages support.I want to set the default locale to Arabic language and change the locale when the user clicks the desired language in JSP page.
Here is my mvc-dispatcher-servlet.xml,
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.benchmark.ushers.presentation.controller"/>
<bean id="internalResourceResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- resource bundle configuration-->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:locale/messages" />
<property name="fallbackToSystemLocale" value="false"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="ar" />
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
<!-- end of resource bundle configuration-->
And my JSP page as below,
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<tiles:insertDefinition name="defaultTemplate">
<tiles:putAttribute name="body">
<div class="body">
<h1>Ushers</h1>
lang : <a href="?lang=en">English</a> | <a
href="?lang=ar">Arabic</a>
<h3>
welcome.springmvc :
<spring:message code="footer.content" text="default text" />
</h3>
<h3>
hello :
<spring:message code="footer.hello" text="default text" />
</h3>
</div>
</tiles:putAttribute>
</tiles:insertDefinition>
I do not know what is wrong in my code while the only displayed the English text only.
Upvotes: 0
Views: 10669
Reputation: 389
In my case, I was using the java config version and it didn't worked until I added the "localeResolver" bean name. The internationalization beans I created are listed below. You can check it works by changing the lang parameter in the URL: /some-page.do?lang=ro
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
...
@Bean(name="localeResolver")
public LocaleContextResolver getLocaleContextResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(Locale.US);
return localeResolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getLocaleChangeInterceptor());
}
@Bean
public LocaleChangeInterceptor getLocaleChangeInterceptor() {
final LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
return interceptor;
}
Upvotes: 0
Reputation: 65
The above configuration in the question is correct. The problem was in the requested page sets as welcome page in web.xml file so it is executed without any interceptors.
Every thing works fine after comment this part in web.xml
<!-- <welcome-file-list>
<welcome-file>/WEB-INF/pages/adminHome.jsp</welcome-file>
</welcome-file-list>-->
Upvotes: 1
Reputation: 1555
I guess you need DefaultAnnotationHandlerMapping
to mapping with @RequestMapping it will check for any locale change request. See also Spring Internationalization.
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
Read this topic to avoid mixing it with <mvc:annotation-driven/>
Upvotes: 0