drenda
drenda

Reputation: 6244

NoSuchMessageException with MessageSource in Spring Boot

I'm using Spring Boot 1.5.4 and Spring Data REST. I put my messages.properties inside resources/i18n folder. In this folder I've two files: messages.properties and messages_it.properties I configured my WebMvcConfigurerAdapter in this way:

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        //messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setCacheSeconds((int) TimeUnit.HOURS.toSeconds(1));
        messageSource.setFallbackToSystemLocale(false);
        return messageSource;
    }

When I have to localize a message I do:

        String message = messageSource.getMessage(throwable.getClass().getName(), new Object[] {}, locale);

This works fine if the properties file for the language exsists, otherwise I've an exception like:

Caused by: org.springframework.context.NoSuchMessageException: No message found under code 'org.springframework.dao.InvalidDataAccessApiUsageException' for locale 'fr_CH'.

My guess is for some reason MessageSource is not reading the messages.properties file also if it is present and it has the same keys of others.

Upvotes: 1

Views: 3591

Answers (1)

xyz
xyz

Reputation: 5407

Usually this exception happens when MessageBundle is not configured or configured in wrong way.

change

messageSource.setBasenames("i18n/messages");

to

messageSource.setBasenames("/i18n/messages"); or messageSource.setBasenames("classpath:/i18n/messages");

if i18n it's root directory for i18n files.

Upvotes: 2

Related Questions