树枝翻译带有标签的短语



Twig 版本 - 最新

树枝扩展版本 - 最新


我想避免在 .po 文件中使用 html 标签

这是短语内带有链接的文本。

<p>{{ 'Click this <a href="/test/">test link</a>, friend' | trans | raw }}</p>

使用本主题中的解决方案 - https://stackoverflow.com/a/11546933/2145125

<p>{{ 'Click this %a_open%test link%a_close%, friend' | trans({'%a_open%' : '<a href="/test/">', '%a_close%' : '</a>'}) | raw }}</p>

有 PHP 警告 Warning: gettext() expects exactly 1 parameter, 2 given

编译的模板 PHP 代码是

echo gettext("Click this %a_open%test link%a_close%, friend", array("%a_open%" => "<a href="/test/">", "%a_close%" => "</a>"));

找到了 2 个解决方案。

trans + replace = gettext() + strtr() 您可以使用命名占位符,如 %a_open% 等,占位符的顺序无关紧要

<p>{{ 'Click this %a_open% test link %a_close% friend' | trans | replace ({"%a_open%" : '<a href="/test/">', "%a_close%" : "</a>"}) | raw }}</p>

trans + format = gettext() + spritf

<p>{{ 'Click this %stest link%s, friend' | trans | format('<a href="/test/">', '</a>') | raw }}</p>

最新更新