Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a custom location for messages properties files in thymeleaf?

Im using thymeleaf and spring, my messages.properties files need to have the same name as the template in order for them to work. I have tried to create a custom path using webConfigurer.Java but it's not working.

WebConfigurer.java source:

...
...
@EnableWebMvc
@EnableTransactionManagement
@Configuration
@ComponentScan({"en.irp.project.*"})
@PropertySource("classpath:/application.properties")
@Import({SecurityConfigurer.class})
public class WebConfigurer extends WebMvcConfigurerAdapter {
    ...
    ...
    ...
    @Bean(name="messageSource")
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
        resource.setBasename("WEB-INF/languages/messages");
        resource.setDefaultEncoding("UTF-8");
        return resource;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("language");
        return localeChangeInterceptor;
    }

    @Bean(name = "localeResolver")
    public SessionLocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(new Locale("en"));
        return localeResolver;
    }
    ...
    ...
}
like image 731
inControl Avatar asked Sep 05 '25 03:09

inControl


1 Answers

Just for future reference.

If Spring Boot is used, you can also set its custom location in application.properties file:

spring.messages.basename=i18n/messages

Javadoc:

Comma-separated list of basenames, each following the ResourceBundle convention. Essentially a fully-qualified classpath location. If it doesn't contain a package qualifier (such as "org.mypackage"), it will be resolved from the classpath root.

like image 192
emrekgn Avatar answered Sep 07 '25 20:09

emrekgn