以正确的方式几乎不可能正确翻译内部具有 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 <a href="#">Link</a> End string 表示英语,另一种国际语言full_formatted_string_id=<a href="#">Link</a> 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 分隔资源字符串。