【发布时间】:2014-01-14 09:09:17
【问题描述】:
我正在使用 Spring 3 和 Thymeleaf 制作一些网页,但我不知道如何显示这样的消息:
welcome.message=您好{0},欢迎!
然后将 {0} 替换为 thymeleaf 标签内的用户名:
<h1 th:text="#{welcome.message}">Welcome Placeholder</h1>
我什至不确定 {0} 是否是捆绑消息的正确语法。
【问题讨论】:
我正在使用 Spring 3 和 Thymeleaf 制作一些网页,但我不知道如何显示这样的消息:
welcome.message=您好{0},欢迎!
然后将 {0} 替换为 thymeleaf 标签内的用户名:
<h1 th:text="#{welcome.message}">Welcome Placeholder</h1>
我什至不确定 {0} 是否是捆绑消息的正确语法。
【问题讨论】:
你可以使用
#{welcome.message(${some.attribute})}
其中some.attribute 将是替换{0} 时使用的值。
您应该能够用逗号分隔 () 之间的值以添加更多要使用的值。
【讨论】:
您甚至可以使用计算出的消息密钥作为参数:
<p th:text="#{messages.msg1(${param1})}"></p>
<p th:text="#{messages.msg2(${param2},${param3})}"></p>
<p th:text="#{messages.msg3(#{${param4}})}"></p>
上面,[msg3]的参数是一个消息键[#{key}],其中key是自己计算的[${param4}]。好处是您可以在国际化消息中插入国际化计算片段。
【讨论】:
如果您需要传递一个不知道数组大小的参数数组,那么您可以使用:
<p th:text="${#messages.msgWithParams(messageKey, messageParams)}"></p>
<!-- or -->
<p th:text="${#messages.msgOrNullWithParams(messageKey, messageParams)}"></p>
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#messages-1
【讨论】: