如何在 Thymeleaf 中设置动态 JSON 字符串

How to set dynamic JSON String in Thymeleaf

我在前端页面中使用 Thymeleaf,出于某种原因,我必须通过 i18next.js 而不是 spring 框架来处理 i18n。

根据i18next-jquery introduction - using options in translation function,我这样写:

<span data-i18n="myKey" data-i18n-options='{ "WHERE": "TW" }'></span>

还有我的语言资源:

{ "myKey" : "User is from {{WHERE}}."  }

而html被完美解析显示如下:

<span data-i18n="myKey" data-i18n-options='{ "WHERE": "TW" }'> User is from TW. </span>

但是当我通过 Thymeleaf 属性动态设置 {{WHERE}} 时,它无法解析任何东西。

我试过了
th:attrappend="data-i18n-options='{WHERE: '+ ${country} +'}'"
还有
th:data-i18n-options="'{WHERE: '+ ${country} +'}'",
不知怎的,它最终变成了这样:

<span data-i18n="myKey" data-i18n-options="{WHERE: TW}">User is from  .</span>

所以我想知道是否有任何想法可以解析 Thymeleaf 属性中的 json 字符串?


更新:
我想分享我的解决方案,我希望它能节省别人的时间:)

th:attrappend='data-i18n-options=|{ "WHERE":"${country}"}|' 或者 th:data-i18n-options='|{ "WHERE":"${country}"}|'

请尝试这两个:

th:attrappend='data-i18n-options=|{ "WHERE":"${country}"}|'
要么
th:data-i18n-options='|{ "WHERE":"${country}"}|'