如何使用 i18n 中的 Thymeleaf 给按钮标签

Posted

技术标签:

【中文标题】如何使用 i18n 中的 Thymeleaf 给按钮标签【英文标题】:How to give button label using Thymeleaf from i18n 【发布时间】:2017-10-04 16:15:45 【问题描述】:

目前我正在尝试用 message_en.properties 文件中的值替换按钮标签中的单词 Save

<input type="submit" value="Save" class="btn btn-primary btn-block"/>

我试过如下:

<input type="submit" th:text="#newuser.page.saveBtn" class="btn btn-primary btn-block"/>

但这并没有给出我预期的结果。请帮我解决问题。

【问题讨论】:

你配置messageSource了吗? 是的,一切正常,除了保存按钮上的标签 【参考方案1】:

不应该这样吗?

<input type="submit"
    th:value="#newuser.page.saveBtn"
    class="btn btn-primary btn-block"/>

【讨论】:

这正是我想要的。谢谢它现在正在工作【参考方案2】:

你可以使用按钮,它对我有用

<button type="submit" class="btn btn-primary btn-block" th:text="#newuser.page.saveBtn"></button>

【讨论】:

是的,这很好用,但我问了这个问题,因为有很多其他 html 文件使用&lt;input type="submit" value="***" class="btn btn-primary btn-block"/&gt;。我想知道有没有办法在这里使用百里香而不改变很多代码。无论如何,谢谢你的回答

以上是关于如何使用 i18n 中的 Thymeleaf 给按钮标签的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 spring-boot 和 thymeleaf 设置标准 i18n 语言环境?

如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n?

Spring Boot i18n + Thymeleaf:在 messages.properties 文件中使用数组

使用Thymeleaf配置国际化页面

带有 Spring Boot 和 thymeleaf 的 Spring MVC 4 - i18n 无法正常工作

i18n 在 spring-boot / thymeleaf 项目中它没有检索本地化消息