Thanthla
Thanthla

Reputation: 586

Spring MessageSource: access message file within dependency jar

I have a Spring MVC application using i18n. Now I want to use a message file provided within a dependency. I want to use the same keys/values to have consistent output in my application. But somehow it does not work... What am I missing?

My ResourceHandler:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/WEB-INF/resources/");
}

My MessageSource:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasenames("messages/user/login/login",
                        "messages/user/user",
                        //external file (NOT WORKING)
                        "classpath:messages/enums/enums");
    source.setDefaultEncoding("UTF-8");
    return source;
}

Location of the message.file starting from the war root:

WEB-INF/lib/utils-0.1.jar/messages/enums/enums_en.properties

Upvotes: 1

Views: 1864

Answers (1)

Thanthla
Thanthla

Reputation: 586

I had to change two things to get it to work:

  1. I needed ReloadableResourceBundleMessageSource and not ResourceBundleMessageSource.
  2. After changing the source class, classpath: had to be added to all base names. There is no difference between the local "resource" files and files inside dependencies.

The final code looks like this:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasenames("classpath:messages/user/login/login",
                        "classpath:messages/user/user",
                        //external file (in utils-0.1.jar)
                        "classpath:messages/enums/enums");
    source.setDefaultEncoding("UTF-8");
    return source;
}

Upvotes: 4

Related Questions