spring i18n: problem with multiple property files

Alternative solution to those already mentioned would be using the property parentMessageSource that delegates the message lookup to the parent if it does not find it in the current instance.

In your case it is probably better to stay with the basenames array. Having the hierarchic message source could make more sense if the message sources were using different implementations. E.g. the second one reading messages from db.

Note that in this case, when Spring finds two instances of MessageSource, so the primary one will be the one with the id messageSource.

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="parentMessageSource"><ref bean="anotherMessageSource"/></property>
    <property name="basename" value="classpath:i18n/messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="anotherMessageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:i18n/newmessages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

For those(like me), looking for java config solution :

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("i18n/messages", "i18n/newmessages");
        return messageSource;
    }

jdoc : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/AbstractResourceBasedMessageSource.html#setBasenames-java.lang.String...-


Another clean way to doing same:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:messages1</value>
                <value>classpath:messages2</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
</bean>

The basenames (s at the end) property accept an array of basenames:

Set an array of basenames, each following the above-mentioned special convention. The associated resource bundles will be checked sequentially when resolving a message code.

@see java doc: ReloadableResourceBundleMessageSource.setBasenames

So you should have only one messages source, with a list files (try to seperatate them by comma).

<bean id="anotherMessageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames" value="classpath:i18n/newmessages,classpath:i18n/messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>