9

I have a multi module Spring project with Maven. I'm using Spring 3.2.3 with annotation config.

I have the following layout:

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

I need that common, module1 and module2 can specify their own i18n properties (and the webapp collects those files and provides them somehow ?!):

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

I tried using

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

But it seems like Spring will just use one of those translation files, but instead it should use all.

Another possibility would be to specify a unique properties file name for each module, but then I don't know what basename to set via messageSource.setBasename(...).

Thanks for your help!

Benjamin M
  • 23,599
  • 32
  • 121
  • 201

3 Answers3

0

I have a message in a module and in the parent basename array i put the classpath

      <property name="basenames">

      <array>

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

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

      </array>
  </property>
  • Ok you have this message, but what to do with it ? Please explain your answer (should they add this to their mvn config file ? Use something similar ? ... ?) – NatNgs Aug 25 '17 at 11:57
0

I am going to explain me better.

Normally we have a file with the url of the files of the new module. For example com.prueba.test.J2EEDeclarations

public final class J2EEDeclarations { public static final String MESSAGES_BASENAME = "/WEB-INF/resources/pruebaUtils.i18n";

With this module we create a jar in our maven repository

And in the parent we import this jar.

After that we only have to change the basename to use the properties or the new module

  <array>

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

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

  </array>

0

Just create a customized class that uses PathMatchingResourcePatternResolver by extending ReloadableResourceBundleMessageSource according to the answer at https://stackoverflow.com/a/27532814/4258376

Then, you will be able to assign that customized class for your MesssageSource with basenames having classpath*:

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