A basic Drupal 8 module for testing translation within Twig {% trans %}
block
see t() variable placeholders not appearing to work in Twig {% trans %}
To run the test with this module:
- Install this module in D8 the normal way
- Enable the 4 multilingual modules
- In
admin/config/regional/language
clickAdd Language
and add Spanish - go to
[path/to/drupal]/trans-test
and then for Spanish[path/to/drupal]/es/trans-test
From the link above:
According to a few sources, the t() variable placeholders are supposed to work in the following way, using a {% trans %}
block:
{% set string = '&"<>' %}
{% trans %}
Escaped: {{ string }} <-- default, same as @ (i.e. escape plain text to HTML)
{% endtrans %}
{% trans %}
Pass-through: {{ string|passthrough }} <-- same as ! (i.e. insert as is)
{% endtrans %}
{% trans %}
Placeholder: {{ string|placeholder }} <-- same as % (i.e. <em class="placeholder">' . escape plain text to HTML . '</em>')
{% endtrans %}
However, running some tests, I am not able to get this to work.
In the output generated by this module, note that tests 2,3&5 are not supposed to produce translations (and they don't), but that tests 1,4,6,7 should do so; however, only test 1 is producing a translation. None of the tests inside {% trans %}...{% endtrans %}
produce translations.
A 2nd thing to note, from looking at the HTML source code, is that for the string '&"<>' the characters are not being escaped (turned into HTML entities), although they might be intended to by tests 1,4,7. (Note that straight Twig does do, this though: {{ string }}
).