Twig "trans" doesn't work
I am using twig tag trans
in my templates and I want to pass variables in it like this:
{% trans with {
'%link_start%': '<a href="http://www.google.nl/">',
'%link_end%': '</a>'
} %}
This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}
But this gives me the following exception, which points to the branch pattern in line {% trans with
:
PHP Fatal error: Throw exception "Twig_Error_Syntax" with message "Unexpected token" punctuation "value" {"(expected" end of statement block "). In [twig-template: line]
Even when I copy and paste the examples from the Symfony documentation , I get the same exception. So I am at a loss what am I doing wrong here?
FYI: I'm using Twig 1.33 with the i18n extension enabled (and I'm not using the Symfony framework)
source to share
Twig doesn't support trans with
out of the box. It is part of the Symfony Translation Extension. This explains why even the official Symfony documentation doesn't work - you are not using Symfony.
See this issue: https://github.com/twigphp/Twig-extensions/issues/74 . There is a pull request for support trans with
, but it has not been merged.
You might want to use the Symfony Translation Component in your application. You can use Symfony Components in your application without even using the full Symfony (Framework) stack.
I haven't tried it, but you can use jhogervorst / Twi18n instead .
source to share
As a workaround, you can use filter
tag with replace
.
{% filter replace({'%foo%': 'blue', '%bar%': 'red'}) %}
{% trans %}
I like %foo% and %bar% messages.
{% endtrans %}
{% endfilter %}
source to share