Spring 如何在多个ResourceBundle之间共享与区域设置无关的属性?

Spring 如何在多个ResourceBundle之间共享与区域设置无关的属性?,spring,localization,Spring,Localization,场景:在应用程序中,我有语言相关的属性文件,这些文件用作生成电子邮件的模板: 电子邮件订阅\u en.properties: email.subject=You are successfully subscribed to list {0} email.body=... email.subject=You are successfully unsubscribed from list {0} email.body=... 电子邮件取消\u en.属性: email.subject=You a

场景:在应用程序中,我有语言相关的属性文件,这些文件用作生成电子邮件的模板:

电子邮件订阅\u en.properties

email.subject=You are successfully subscribed to list {0}
email.body=...
email.subject=You are successfully unsubscribed from list {0}
email.body=...
电子邮件取消\u en.属性

email.subject=You are successfully subscribed to list {0}
email.body=...
email.subject=You are successfully unsubscribed from list {0}
email.body=...
等等。现在,在Spring环境中,我希望有以下捆绑包:

<bean id="subscriptionMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="org.company.email-subscription" />
</bean>

<bean id="cancellationMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="org.company.email-cancellation" />
</bean>

与我希望在上下文中声明的这些公共语言独立属性合并:

<util:properties id="commonMailProperties">
    <prop key="email.from">noreply@company.org</prop>
    <prop key="email.to">{0}@company.org</prop>
</util:properties>

noreply@company.org
{0}@company.org

怎么可能呢?

据我所知,这一点不受支持。您正在尝试将配置与资源包混合使用。我觉得你现在拥有的是正确的。如果你没有保持现状的奢侈,这里有一个方法(更像是一个黑客)

  • 实现
    org.springframework.context.MessageSource
    ,将“commonMailProperties”(java.util.Properties)作为依赖项,并将bean id称为“commonMessageSource”

  • 在“getMessage”实现中,从“commonMailProperties”获取值

  • 将“commonMessageSource”注入到“subscriptionMailProperties”和“cancellationMailProperties”的“parentMessageSource”属性中


  • 如果有人对完整的解决方案感兴趣:

    • 创建类
      属性消息源

      /**
       * {@link org.springframework.context.MessageSource} implementation that resolves messages via underlying
       * {@link Properties}.
       */
      public class PropertiesMessageSource extends AbstractMessageSource {
      
          private Properties  properties;
      
          /**
           * Set properties to use.
           */
          public void setProperties(Properties properties) {
              this.properties = properties;
          }
      
          @Override
          protected MessageFormat resolveCode(String code, Locale locale) {
              String property = properties.getProperty(code);
      
              if (property == null) {
                  return null;
              }
      
              return createMessageFormat(property, locale);
          }
      }
      
    • 使用它:

      <bean id="commonMailProperties" class="org.company.PropertiesMessageSource">
          <property name="properties">
              <props>
                  <prop key="email.from">noreply@company.org</prop>
                  <prop key="email.to">{0}@company.org</prop>
              </props>
          </property>
      </bean>
      <bean id="subscriptionMailProperties" class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="org.company.email-subscription" />
          <property name="parentMessageSource">
              <ref bean="commonMailProperties"/>
          </property>
      </bean>
      
      
      noreply@company.org
      {0}@company.org
      

    ResourceBundleMessageSource
    (更确切地说:所有
    AbstractMessageSource
    的后代)现在都有
    commonMessages
    属性,该属性可以保存与区域设置无关的值。例如,虽然您希望邮件主题和正文区域设置依赖,但某些属性(邮件发件人和邮件收件人)在所有捆绑包中都是通用的(请检查):

    
    empty@mydomain.org
    %s@mydomain.org