Custom Spring Boot starter: how do you contribute i18n messages to the MessageSource?
internationalization, java, spring, spring-boot
Solution
Maybe it is long shot but you can try to use BeanFactoryPostProcessor.
Idea is following:
Take "messageSource" bean out of the application context. Note that it might but does not have to be a spring boot's one if e.g. developer wants to use its own implementation and don't use spring boot autoconfiguration.
Replace it with your own implementation that tries to resolve "your keys" and the rest delegate to original message source. Or vice versa if you want to make possible to override your translations by developer (there can be problems if original message source does not throw exception for unknown keys).
But there might be a better way to do that.
Problem
I'm writing a custom Spring Boot starter that other developers will drop into their applications, and this starter contains out-of-the-box controllers and UI screens. These UI screens are internationalized and the i18n keys/values are in a package file: `com/foo/wherever/i18n.properties`. I want to ensure that when my starter is loaded at startup, that these i18n.properties are available in the application's `MessageSource` automatically so that my UI pages work (rendered via normal Spring Controller + ViewResolver + View implementations) without the app developer having to specify this file themselves. In other words, they should be able to add my starter to their runtime classpath and everything 'just works' without the need to configure anything. Now, I have discovered that the app developer can create their own `src/main/resources/messages.properties` file and manually configure the additional messages file in `application.properties`: ``` spring.messages.basename = messages, com.foo.wherever.i18n ``` And this will work. However, this requires both of the following: - They must manually configure the `spring.messages.basename` property - it's not automatic. and - They must have their own `messages.properties` file in their application classpath. If a `messages.properties` file does not exist, `spring.messages.basename` doesn't even work. Even if they don't care about i18n, this is still required - not desirable. I suppose I could move my i18n.properties file to a classpath:/messages.properties file in the starter .jar, but that doesn't seem like a good solution: if the app dev has their own messages.properties file only one of them would be read, resulting in missing message values. It seems as if Spring Boot MessageSourceAutoConfiguration should have a concept of a `CompositeMessageSource` that iterates over one or more `MessageSource` instances that are available (and `Order`ed) in the Spring ApplicationContext and that is used by the DispatcherServlet. This would allow any starter to contribute to the available messages just by declaring a `MessageSource` in their auto config Is it possible to do what I ask? What is the most 'hands off' solution for the app developer?