Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring i18n: problem with multiple property files

My messages.properties is really a big file. So, I tried moving some of the properties in messages.properties to a new file, say newmessages.properties and updated spring bean configuration xml with both the files as follows:

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <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>

But, I am not able access any properties defined in the new property file. Is it really possible to specify multiple property files(for a single locale)?

like image 975
Arjun Avatar asked Apr 11 '11 07:04

Arjun


3 Answers

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>
like image 116
Ralph Avatar answered Nov 03 '22 22:11

Ralph


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>
like image 26
spgodara Avatar answered Nov 04 '22 00:11

spgodara


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>
like image 40
David L. Avatar answered Nov 03 '22 22:11

David L.