Adding a link inside translated content in Twig template

后端 未结 4 1557
余生分开走
余生分开走 2021-02-07 00:41

Inside a Twig template I would need to have a translated text that contains a link (the path should be generated by the Router, not statically embedded). Twig does not allow to

4条回答
  •  萌比男神i
    2021-02-07 01:22

    rebdirdo's solution is not really safe as it's not escaping whole message. It is not working correctly for messages like "don't use tag, use tag instead. %link_start%Here%link_end% you can find why.", because the tags will be not escaped and will be not visible.

    working approach:

    translation file:

    advises.strong: don't use tag, use tag instead. %link_start%Here%link_end% you can find why.

    twig file:

    {{ 'advises.strong'|trans|nl2br|replace({'%link_start%': '', '%link_end%': ''})|raw }}

    Note the nl2br filter. It is necessary to put some filter there to make the raw filter working only for the link tags.

提交回复
热议问题