首页 > 解决方案 > 使用内部国际化元素

问题描述

我正在使用 Spring Boot + Thymeleaf。我想国际化这样的事情:

<p>Already registered? <span class="link">Log In</span></p>

如果我添加th:text="#{prompt}"<p>标签,内部span将被属性值替换。

有没有办法用我的资源包中的一个属性来国际化整个元素的文本?<p>(可能有占位符或者我不知道)

标签: springspring-bootinternationalizationthymeleafproperties-file

解决方案


您可以在属性值内为 html 标签添加占位符

财产:

prompt = Already registered? {0}Log In{1};

html:

<p th:utext="#{prompt('<span class=link>', '</span>')}"></p>

注意:我使用th:utext而不是th:text因为它不会转义 html。

但是如果你只有两个不同的属性会更清楚。例如:

<p>
    <th:block th:text='#{prompt1}'></th:block>
    <span class='link' th:text='#{prompt2}'></span>
</p>

推荐阅读