In Drupal 8, with English and German enabled, I am trying to translate a variable using a specific context.
When visiting a page in German:
{% trans with {'context': 'Long month name'} %}
March
{% endtrans %}
This outputs März which is fine.
{% set month = 'March' %}
{% trans with {'context': 'Long month name'} %}
{{ month }}
{% endtrans %}
This outputs March which is not fine.
Why? And how am I supposed to translate variables in a Twig template?
Here is the documentation I have read.
Edit: https://www.drupal.org/node/2357633
This mentions:
The
t
filter will run the variable through the Drupalt()
function, which will return a translated string. This filter should be used for any interface strings manually placed in the template that will appear for users.
And also:
The following patterns are unsafe and should not be used because they pass a variable directly to translation. This not only inflates the list of strings for translation but is also a potential vulnerability, particularly if the output can be entered by a user.
{{ var1|t }}
{{ var1|placeholder }}
{% trans %}{{ var1 }}{% endtrans %}