thymeleaf 国际化使用默认文本 html

Thi*_*aut 2 spring internationalization thymeleaf spring-boot

我将 thymeleaf 与 Spring Boot 一起使用,

<span th:text="#{home.welcome}">Welcome Default Message</span>
Run Code Online (Sandbox Code Playgroud)

当在 messages.properties 中找不到密钥时,我有

??home.welcome_en_US??
Run Code Online (Sandbox Code Playgroud)

我不想翻译 messages.properties 中的每条消息。当找不到翻译关键消息时,我希望在 html 页面中写入“欢迎默认消息”。

我怎样才能做到这一点?

小智 5

也许不再相关,但以下内容对我有用。我正在运行 Thymeleaf 3.0.7.RELEASE。

<span th:text="${#strings.defaultString(#messages.msgOrNull('home.welcome'),'Welcome Default Message')}"/>
Run Code Online (Sandbox Code Playgroud)

如果未找到消息,则将 null 传递给 #strings.defaultString 调用,当给定 null 参数时,该调用将返回“欢迎默认消息”。

更多信息请参见http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#strings