spring i18n:多个属性文件的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5618071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:25:32  来源:igfitidea点击:

spring i18n: problem with multiple property files

springinternationalization

提问by Arjun

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:

我的messages.properties 确实是一个大文件。因此,我尝试将 messages.properties 中的一些属性移动到一个新文件中,例如 newmessages.properties 并使用这两个文件更新 spring bean 配置 xml,如下所示:

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

但是,我无法访问新属性文件中定义的任何属性。是否真的可以指定多个属性文件(针对单个语言环境)?

回答by Ralph

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

basenames (s在最后) 属性接受一个 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

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

回答by spgodara

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>

回答by David L.

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

已经提到的替代解决方案是使用属性parentMessageSource,如果在当前实例中找不到它,则将消息查找委托给父级。

In your case it is probably better to stay with the basenamesarray. 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.

在您的情况下,最好留在basenames数组中。如果消息源使用不同的实现,拥有分层消息源可能更有意义。例如,第二个从 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.

请注意,在这种情况下,当 Spring 找到 的两个实例时MessageSource,主要的将是具有 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>

回答by Himanshu Tyagi

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

对于那些(像我一样),正在寻找 java 配置解决方案:

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

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