Search code examples
springspring-bootinternationalizationapplication.properties

Spring Boot i18n in multi modules project


I have this multi modules project:

Parent Pom
|
|----Main module (@SpringBootApplcation and application.properties)
|
|----Module I
|
|----Module II
|
|----Module III

The main module depends on other modules and contains messages.properties and messages_fr.properties under resources/messages/ and also the application.properties where I defined MessageResource bean spring.messages.basename=messages/messages. Now I would like to put the messages.properties files in each module. Eg for Module I, under src/main/resources/messages/messages.properties same thing for Module II. My question is, how can I add the module's messages in the MessageResource bean defined in application.properties? I tried several declarations like these:

spring.messages.basename=messages/messages,classpath:/com/company/moduleI/resources/messages/messages or spring.messages.basename=messages/messages,classpath:moduleI/resources/messages/messages but none works. Is there a way to achieve this?


Solution

  • I finally found a better solution, using the ResourceBundleMessageSource#addBasenames(...) to add all modules' messages.

    EDIT

    In each module, I renamed the message files, e.g. for Module I, I had resources/messages/moduleI-messages.properties, resources/messages/moduleI-messages_en.properties. Then I defined a configuration class in that module to add the messages source files.

    @Configuration
    public class ModuleConfig {
        private final ResourceBundleMessageSource messageSource;
    
        @PostConstruct
        public void addMessageBaseName() {
            messageSource.addBasenames( "messages/module1-messages" );
        }
    }
    
    

    I am sure there could be a better way to go but I didn't find it.