n00b n00b - 1 month ago 12
Java Question

Locale only getting values for default locale while other locale is not displaying

hi I am working on spring locale but it is still getting values from one locale not from other here is my code so far
dispatcherServlet

<mvc:annotation-driven enable-matrix-variables="true" />
<context:component-scan base-package="com.*" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/com/resources/messages_en</value>
<value>/com/resources/messages_nl</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
</mvc:interceptors>


and this is I am using in my page.jsp

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<p>
<b><h3>
<a href="?language=en">English</a>| <a href="?language=nl">Dutch</a>
</h3></b>
</p>
<spring:message code="tagline"></spring:message>
<hr>
</body>
</html>


For English, everything works fine but while clicking on
Dutch
its also showing English too, how to resolve that?

this is only data in my files :

1 message_en.properties
tagline=This is English

2 message_nl.properties
tagline=This is dutch


Please Help

Answer

Do you have <mvc:annotation-driven /> in you config. Also your <mvc:interceptors> should be like below.

 <mvc:interceptors>
     <mvc:interceptor>
        <mvc:mapping path="/**" />
          <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
              <property name="paramName" value="language" />
          </bean>
   </mvc:interceptor>
</mvc:interceptors>

And MessageSource bean

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

Your properties file should be like below.

messages.properties
messages_en_US.properties
messages_zh_CN.properties

Check this post.