spring i18n: problem with multiple property files

28,327

Solution 1

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>

Solution 2

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>

Solution 3

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>

Solution 4

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...-

Share:
28,327

Related videos on Youtube

Arjun
Author by

Arjun

Java Developer.

Updated on July 09, 2022

Comments

  • Arjun
    Arjun almost 2 years

    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)?

  • Piyush Mittal
    Piyush Mittal about 8 years
    say i have message1_en,message2_en and m sending en as locale that time how spring will come to know which file to take?? @spgodara

Related