Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display messages in Thymeleaf and Spring Boot?

Tags:

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).

like image 412
stevecross Avatar asked Apr 09 '15 07:04

stevecross


People also ask

How do you display text in Thymeleaf?

In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> . Note that, the text inside p tag won't be shown.


2 Answers

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 
like image 59
stevecross Avatar answered Sep 18 '22 18:09

stevecross


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

like image 45
Faraj Farook Avatar answered Sep 20 '22 18:09

Faraj Farook