如何在 Thymeleaf 和 Spring Boot 中显示消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29532172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:30:05  来源:igfitidea点击:

How to display messages in Thymeleaf and Spring Boot?

springspring-mvcspring-bootthymeleaf

提问by stevecross

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

我创建了一个使用 Thymeleaf 作为模板引擎的 Spring Boot Web 应用程序。我配置了MessageSource在子文件夹中查找消息:

@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.propertieswith the content ticket.type.BUG=Fehler. In my template I try to display the text like this:

在这个文件夹中,我创建了messages_de.properties包含内容的文件ticket.type.BUG=Fehler。在我的模板中,我尝试显示这样的文本:

<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?

我错过了什么?我是否必须配置任何额外的 bean?

P.S.:

PS:

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

在“服务器端”,我可以使用MessageSource#getMessage("ticket.type.BUG", null, Locale.GERMANY).

回答by stevecross

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

因为我使用的MessageSource是Spring Boot,所以配置了一个MessageSourceAutoConfiguration. 这些设置可以在application.properties文件中轻松更改。就我而言,我必须将以下内容添加到此文件中:

spring.messages.basename=i18n/messages

回答by Faraj Farook

And add this to your application.propertiesfile

并将其添加到您的application.properties文件中

#messages
spring.messages.basename=i18n/messages

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

并将文件存储在上面指定的正确文件夹中。

you don't need messageSourcebean

你不需要messageSource

回答by Michael RH?se

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

我解决 i18n 消息传递的方法是MessagesSource像您一样定义bean。此外,我不得不重写getValidator()的方法WebMvcConfigurerAdapter

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

After this, it worked for me.

在此之后,它对我有用。

回答by fkurth

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

是否存在允许回退的文件messages.properties(否_de)?它有效吗?您的浏览器是否设置为 locale DE_de

回答by Paul John

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

这是我为 thymeleaf 和 spring 参考的教程 = http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html