首页 > 解决方案 > 从控制器返回的值的弹簧内部化

问题描述

我从控制器返回了这条消息,即

model.addAttribute("Message", "This is the English version of this website");

目前它已使用 line 显示

[[${Message}]]

这将显示上面的消息。如何使上述消息值内部化.so 消息更改为不同的语言

像这样的东西

th:text="#{home.page.title}" >> 将显示标题是许多语言

[[${Message}]] 多语言怎么办

标签: springspring-bootspring-mvc

解决方案


请尝试以下步骤:

  1. messages.properties

    labels.my_label=这是本网站的英文版

  2. 在控制器中:

    model.addAttribute("消息", "labels.my_label");

  3. 在 html (Thymeleaf) 中:

    <span th:text="#{__${Message}__}"></span>

或者您可以直接输出为:<span>[[#{__${Message}__}]]</span>

  1. 如果您想要 i18n:
    在属性文件中添加国家/地区,例如:(messages_en.properties英语)、messages_vi.properties(越南语)、message_jp.properties(日语)、...
    In messages_en.properties:
    labels.my_label=This is the English version of this website
    In messages_<lang>.properties:
    labels.my_label=Another特定语言的文本

参考:
https ://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-thtext-and-externalizing-text


推荐阅读