【问题标题】:Spring multi module i18n, with modules extending the messageSource contentsSpring 多模块 i18n,带有扩展 messageSource 内容的模块
【发布时间】:2013-07-13 17:33:13
【问题描述】:

我有一个带有 Maven 的多模块 Spring 项目。我正在使用带有注释配置的 Spring 3.2.3。

我有以下布局:

parent
    common  (depends on parent)
    webapp  (depends on parent, common, module1, module2)
    module1 (depends on parent)
    module2 (depends on parent)

我需要 commonmodule1module2 可以指定它们自己的 i18n 属性(webapp 收集这些文件并以某种方式提供它们?!):

common:  src/main/resources/i18n/messages_en.properties
module1: src/main/resources/i18n/messages_en.properties
module2: src/main/resources/i18n/messages_en.properties

我尝试过使用

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/i18n/messages");
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setUseCodeAsDefaultMessage(true);
    return messageSource;
}

但似乎 Spring 只会使用这些翻译文件中的一个,但它应该使用所有。

另一种可能性是为每个模块指定一个唯一的属性文件名,但是我不知道要通过messageSource.setBasename(...) 设置什么basename

感谢您的帮助!

【问题讨论】:

  • 我也有同样的问题。 =(

标签: spring spring-mvc internationalization spring-3 multi-module


【解决方案1】:

我在模块中有一条消息,在父基名数组中我放置了类路径

      <property name="basenames">

      <array>

        <value>/WEB-INF/resources/xxx.i18n</value>

          <value>#{T(com.prueba.test.J2EEDeclarations).MESSAGES_BASENAME}</value>

      </array>
  </property>

【讨论】:

  • 好的,你收到了这条信息,但是怎么处理呢?请解释您的答案(他们是否应该将其添加到他们的 mvn 配置文件中?使用类似的东西?...?)
【解决方案2】:

我会更好地解释我。

通常我们有一个文件,其中包含新模块文件的 url。例如 com.prueba.test.J2EEDeclarations

公共最终类 J2EEDeclarations { public static final String MESSAGES_BASENAME = "/WEB-INF/resources/pruebaUtils.i18n";

通过这个模块,我们在我们的 maven 存储库中创建一个 jar

我们在父目录中导入这个 jar。

之后,我们只需更改基本名称即可使用属性或新模块

  <array>

    <value>/WEB-INF/resources/xxx.i18n</value>

      <value>#{T(com.prueba.test.J2EEDeclarations).MESSAGES_BASENAME}</value>

  </array>

【讨论】:

    【解决方案3】:

    只需通过扩展 ReloadableResourceBundleMessageSource 创建一个使用 PathMatchingResourcePatternResolver 的自定义类 根据 答案在https://stackoverflow.com/a/27532814/4258376

    然后,您将能够为您的 MessageSource 分配该自定义类,其基名具有类路径*:

        @Bean
        public MessageSource messageSource() {
            SmReloadableResourceBundleMessageSource messageSource = new SmReloadableResourceBundleMessageSource();
            messageSource.setBasenames("classpath*:i18n/messages");
            messageSource.setDefaultEncoding("UTF-8");
            return messageSource;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 2016-07-10
      • 2011-06-04
      • 1970-01-01
      • 2021-02-10
      • 2019-08-26
      相关资源
      最近更新 更多