Reputation: 23
I have a question about MessageSource messages in Spring Boot (1.4.1) and Thymeleaf (3).
File app.properties
app.quotes=Francesco's title
app.quotes2=Francesco''s title
In page.html when I print the messages
<h2 th:text="#{app.quotes}"></h2>
<h2 th:utext="#{app.quotes}"></h2>
<h2 th:text="#{app.quotes2}"></h2>
<h2 th:utext="#{app.quotes2}"></h2>
I get exactly (th:text or th:utext does not make any difference)
In my controller
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private MessageSource messages;
@RequestMapping(value="/page", method = RequestMethod.GET)
public String page() {
String text = messages.getMessage("app.quotes", null, LocaleContextHolder.getLocale());
String text2 = messages.getMessage("app.quotes2", null, LocaleContextHolder.getLocale());
log.debug("text = " + text);
log.debug("text2 = " + text2);
// Output
return "page";
}
The text I get logged is
text = Francescos title
text2 = Francesco's title
This is predictable because single quotes in properties messages must be escaped with double single quotes ("Francesco''s title" is supposed to be the correct text). How can I make Thymeleaf to print the message escaping the double single quotes as MessageSource does, or MessageSource to return the plain text as Thymeleaf does? I would like not to use different keys/values based on the caller.
Thanks in advance for any help.
Upvotes: 2
Views: 1058
Reputation: 11017
Spring will not parse the message as long as it not contains an argument (app.quote=John's message
) but will do so if it has a one (app.quote={0}'s message
)
You can override this behavior with setAlwaysUseMessageFormat
:
@Bean
MessageSource defaultMessageSource(){
org.springframework.context.support.ReloadableResourceBundleMessageSource source = new org.springframework.context.support.ReloadableResourceBundleMessageSource();
source.setAlwaysUseMessageFormat(true);
return source;
}
Also take look at resolveCodeWithoutArguments.
Upvotes: 1