Spring multi module i18n, with modules extending the messageSource contents
Asked Answered
B

3

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!

Badgett answered 15/7, 2013 at 18:28 Comment(1)
I have the same problem. =(Feverwort
L
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>
Lurette answered 25/8, 2017 at 11:26 Comment(1)
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 ? ... ?)Auld
L
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>

Lurette answered 25/8, 2017 at 12:17 Comment(0)
L
0

Just create a customized class that uses PathMatchingResourcePatternResolver by extending ReloadableResourceBundleMessageSource according to the answer at https://mcmap.net/q/612078/-does-spring-messagesource-support-multiple-class-path

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;
    }
Lory answered 20/6, 2019 at 3:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.