4

I've changed default locale in spring configuration, but spring always uses messages_en.properties instead of messages.properties.It seems that Spring is ignoring my choice of locale.

defined locases:

messages.properties
messages_en.properties

Spring configuration: application-context.xml

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

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

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list>
      <value>messages</value>
    </list>
  </property>
  <property name="defaultEncoding" value="UTF-8" /> 
</bean>

servlet-context.xml

<mvc:interceptors>
 <mvc:interceptor>
  <mvc:mapping path="/**" />
  <exclude-mapping path="/admin/**"/>
  <exclude-mapping path="/image/**"/>
  <exclude-mapping path="/ajax/**"/>
  <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>

In JSP page

<spring:message code="csn.terminology.csnId" />
<p>Current Locale : ${pageContext.response.locale}</p> 
<!-- output is 'cs', but messages are from messages_en.properties file --> 

In project is used Spring Framework 3.2.4 Thank you in advance for your help.

Peter Jurkovic
  • 2,686
  • 6
  • 36
  • 55

2 Answers2

4

if it's the complete configuration then you forgot to add the locale resolver You can add a SessionLocaleResolver like this and set the default locale property

<bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en"></property>
    </bean>
noobandy
  • 181
  • 4
  • 3
    then there is just one explanation for this ResourceBundleMessageSource has a property named **setFallbackToSystemLocale** [link](http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/context/support/ResourceBundleMessageSource.html) which is default to true so if your locale file for locale cs doesn't contains message for a code then Your system locale is used. – noobandy Sep 30 '13 at 14:32
0

When locale is null, AbstractMessageSource calls Locale.getDefault()

So you should set set default locale as follow:

   @Bean
   public MessageSource messageSource() {
      ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();
      messageSource.setBasename("messages");
      // SET DEFAULT LOCALE AS WELL
      Locale.setDefault(Locale.US);
      return messageSource;
   }
Elie Nehmé
  • 229
  • 2
  • 5
  • Hi Elie, this is probably not the desired solution, as you would set the JVMs default locale with `Locale.setDefault(Locale.US);` (https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#setDefault(java.util.Locale)) This might have a lot of unwanted consequences in a larger application – Sebastian Forza Jul 22 '20 at 11:22