开发者

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.ReloadableResourceBundleMessageSour开发者_JS百科ce">
    <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)?


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>


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>


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


I've successfully loaded multiple messages properties into a spring boot application with following bean configuration.

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource
            = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/messages/exception/messages",
            "classpath:/messages/response/messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜