Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MessageSource seems to ignore property fallbackToSystemLocale

I have problem configuring Spring MessageSource to ignore my system locale. When I call getMessage with null locale parameter I want my MessageSource to choose default property file messages.properties. Instead it chooses messages_en.properties. When I change the name of this property file to messages_fr.properties then default property file is choosen. My system locale is 'en'. So it seems like MessageSource ignores the fallbackToSystemLocale property which I set to false.

This behavior is same with Spring version 4.1.4 as well as 4.1.5.

MessageSource configuration:

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

Getting message:

String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);

Thanks for any advice!

like image 445
Oliver Pentek Avatar asked Mar 25 '15 07:03

Oliver Pentek


1 Answers

fallbackToSystemLocale is NOT intended to steer what the message source does when you invoke them with locale=null

fallbackToSystemLocale control what to do when you request a message (code) that does not exist for the requested local - either because there is no message properties file for the language at all, or just because the message file does not contain the message code

On the other hand when you invoke getMessage with locale=null (messageSource.getMessage("key", null);) then locale will be set by Locale.getDefault

org.springframework.context.support.AbstractMessageSource:

protected String getMessageInternal(String code, Object[] args, Locale locale) {
    ...
    if (locale == null) {
        locale = Locale.getDefault();
    }
    ...

BEFORE the fallbackToSystemLocale property is taken in account.

Therfore the easyest hack-arround (It is not a workarround it is a hack), would be using a language that you not support instead of null: messageSource.getMessage("key", new Locale("XX"));

like image 66
Ralph Avatar answered Sep 27 '22 22:09

Ralph