如何在Thymeleaf和Spring Boot中显示消息? [英] How to display messages in Thymeleaf and Spring Boot?
问题描述
我创建了一个使用Thymeleaf作为模板引擎的Spring Boot Web应用程序.我将MessageSource
配置为在子文件夹中查找消息:
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;
}
在此文件夹中,我创建了内容为ticket.type.BUG=Fehler
的文件messages_de.properties
.在我的模板中,我尝试显示如下文本:
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>
我想念什么?我是否需要配置任何其他bean?
What am I missing? Do I have to configure any additional beans?
PS:
在服务器端",我可以使用MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY)
来获取消息.
On the 'server side' I am able to obtain the message using MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY)
.
推荐答案
由于我使用的是Spring Boot,因此MessageSource
配置了
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
这篇关于如何在Thymeleaf和Spring Boot中显示消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!