21
votes

I created a Spring Boot web application that uses Thymeleaf as the template engine. I configured the MessageSource to look for messages in a subfolder:

@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

    messageSource.setBasename("i18n/messages");
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setCacheSeconds(0);

    return messageSource;
}

In this folder I created the file messages_de.properties with the content ticket.type.BUG=Fehler. In my template I try to display the text like this:

<p th:text="#{ticket.type.BUG}">BUG</p>

But when the page is rendered, I get the following:

<p>??ticket.type.BUG_de_DE??</p>

What am I missing? Do I have to configure any additional beans?

P.S.:

On the 'server side' I am able to obtain the message using MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY).

5

5 Answers

29
votes

Because I am using Spring Boot, the MessageSource is configured with a MessageSourceAutoConfiguration. These settings can be easily changed in the application.properties file. In my case I had to add the following to this file:

spring.messages.basename=i18n/messages
4
votes

And add this to your application.properties file

#messages
spring.messages.basename=i18n/messages

and store the file n the correct folder as specified above.

you don't need messageSource bean

3
votes

The way I resolved the i18n messaging was to define the MessagesSource bean like you. Additionally I had to override the getValidator() method of the WebMvcConfigurerAdapter.

@Override
public Validator getValidator() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setValidationMessageSource( messageSource() );
    return validator;
}

After this, it worked for me.

1
votes

Is there a file messages.properties (no _de) present to allow fallback? Does it work? Is your browser set to locale DE_de ?

0
votes

The message source be relative to classpath:

messageSource.setBasename("  classpath:i18n/messages");

Here is a tutorial I referenced for thymeleaf and spring = http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html