从控制器返回的值的弹簧内部化

时间:2019-02-21 07:23:01

标签: spring spring-boot spring-mvc

我有从控制器返回的消息,即

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

当前使用第

行显示
[[${Message}]]

将显示在上面的消息中。如何使上面的消息值内部化。因此消息更改为不同的语言

类似这样的东西

th:text =“#{home.page.title}” >>将显示标题为多语言

[[$ {Message}]]多语言的操作方法

1 个答案:

答案 0 :(得分:0)

请尝试以下步骤:

  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(日语),...
    messages_en.properties中:
    labels.my_label =这是该网站的英文版本
    messages_<lang>.properties中:
    labels.my_label =特定语言的另一种文字

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

相关问题