【问题标题】:How to use links (anchor tag) inside translations in Thymeleaf?如何在 Thymeleaf 的翻译中使用链接(锚标记)?
【发布时间】:2021-06-14 19:13:49
【问题描述】:

我有以下内容要显示,“here”需要链接。

Hi, your password has been changed, Click here if it is not intended.

进行交易的最佳方式是什么? 我们是否需要分别翻译“Hi, your password has been changed, Click”和“if it is notexpected.

或任何其他在百里香中最简单的方法?

【问题讨论】:

    标签: spring-boot templates hyperlink thymeleaf


    【解决方案1】:

    以正确的方式几乎不可能正确翻译内部具有 HTML 格式的字符串。什么是正确的方法? Thymeleaf 应将翻译后的资源字符串作为 转义文本 (th:text) 使用属性 ...

    放入 HTML 标记中
    <div th:text="#{full_string_id}">Start string Link End string</div>
    

    在哪里full_string_id=Start string Link End string

    这种特殊的结构完全可以翻译成任何国际语言,包括 RTL。只要您开始将完整的句子放到片段上以获得每个片段的特殊格式,您就会遇到一些国际语言的麻烦,包括 RTL。比如用...表示的英文句子

    <span th:text="#{first_string_id}">Start string</span>
    <a href="#" th:text="#{link_string_id}">Link</span>
    <span th:text="#{last_string_id}">End string</span>
    

    但另一个国际语言句子可能有正确的翻译为Link End string Start string

    所以你无能为力吗?当然你有一些解决方案......

    例如,您可以使用非转义文本 (th:utext) 并将字符串格式保留在资源中。

    <div th:utext="#{full_formatted_string_id}">Start string <a href="#">Link</a> End string</div>
    

    其中full_formatted_string_id=Start string &lt;a href="#"&gt;Link&lt;/a&gt; End string 表示英语,另一种国际语言full_formatted_string_id=&lt;a href="#"&gt;Link&lt;/a&gt; End string Start string

    在我看来,最好的解决方案是最简单的一个:不要将格式设置到资源字符串中,而只是改写你的句子。在这种情况下,您仍然可以使用转义文本(因此,您不会损害安全性),并且任何国际语言的翻译都可以工作。例如,不要使用Hi, your password has been changed, Click here if it is not intended.,而是使用Hi, your password has been changed. if it is not intended, please click the following link: 之类的内容并为Link title 分隔资源字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      相关资源
      最近更新 更多