crm86
crm86

Reputation: 1394

i18n works with Thymeleaf but not with MessageSource

I am working with Spring MVC+Thymeleaf and getting the following exception when I tried to get a message from de the MessageSource: NoSuchMessageException

My configuration:

@Bean(name="messageSource")
    public MessageSource messageSource(){
        ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
        source.setBasename("classpath:i18n/messages");
        source.setUseCodeAsDefaultMessage(true);    
        source.setDefaultEncoding( propWebEncoding );
        source.setCacheSeconds(0); /* check the last-modified timestamp */
        return source;
    }

My files are under:

src/main/resources/i18n/messages_es.properties
src/main/resources/i18n/messages_en.properties

My locale is “en” When I use the message “myproperty.example” in HTML (Thymeleaf) it works fine but not when I try to get the message in my @Controller or @Service with:

@Autowired MessageSource messageSource;

And

messageSource.getMessage(“myproperty.example”, null, Locale.EN);

It raises NoSuchMessageException exception impossible to find … for locale ‘en’ This is all configured with maven and my i18n files are in the target folder under: WEB-INF/classes/i18n/ [files]

What I am doing wrong?

Upvotes: 0

Views: 632

Answers (1)

crm86
crm86

Reputation: 1394

The problem: I was working in two different contexts. I was calling MessageSource in the @Service tier (getRootConfigClasses) and it was configured in the ServletConfigClasses.

Solution: Only work in the root context (getRootConfigClasses)

Upvotes: 1

Related Questions