spring - 从控制器返回的值的弹簧内部化
问题描述
我从控制器返回了这条消息,即
model.addAttribute("Message", "This is the English version of this website");
目前它已使用 line 显示
[[${Message}]]
这将显示上面的消息。如何使上述消息值内部化.so 消息更改为不同的语言
像这样的东西
th:text="#{home.page.title}" >> 将显示标题是许多语言
[[${Message}]] 多语言怎么办
解决方案
请尝试以下步骤:
在
messages.properties
:labels.my_label=这是本网站的英文版
在控制器中:
model.addAttribute("消息", "labels.my_label");
在 html (Thymeleaf) 中:
<span th:text="#{__${Message}__}"></span>
或者您可以直接输出为:<span>[[#{__${Message}__}]]</span>
- 如果您想要 i18n:
在属性文件中添加国家/地区,例如:(messages_en.properties
英语)、messages_vi.properties
(越南语)、message_jp.properties
(日语)、...
Inmessages_en.properties
:
labels.my_label=This is the English version of this website
Inmessages_<lang>.properties
:
labels.my_label=Another特定语言的文本
参考:
https ://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-thtext-and-externalizing-text
推荐阅读
- html - 由于内容安全策略,无法使用 iFrame 加载 URL
- text - 按单词(字符)计数拆分文本文件
- xml - 如何在 Xslt 中使用这一行以及如何对列表进行排序以在使用 xlst 转换为 xhtm 时获得正确通道下的信息
- javascript - 如何更改 Phaser 3 中图像的 hitbox 形状?
- windows - 如何在 Docker-Windows 中查看文件结构?
- docker - Docker - 更新时服务器中的 Mantain 文件夹和文件
- javascript - 使用 VBA 宏从网站下载 URL,但它没有扩展名
- python - 如何将 lambda 函数连续应用于 2 个值(lambda> 缺少 1 个必需参数)?
- json - 如何在 Perl 中使用 REST API 发布 json 文件?
- javascript - 为什么 Js 将变量更改为 Nan 并弄乱其他向量