Use the trans tag in Twig without escaping HTML I would like to use variables containing HTML inside a trans tag in my Twig template. (The variables contain the start and the end of a link which appears at different positions in the sentence for different languages.)

There is some documentation about "Drupal 8 Twig Templates and Translations" which explains that for variables inside the trans tag there is a |placeholder-filter and a |raw-filter (for older versions: |passthrough).

I see no difference in the output using these filters.
Here is my template:

{% set string = '&lt;strong&gt;&amp;amp;&lt;/strong&gt;' %}
&lt;div&gt;
  {% trans %}
    Escaped: {{ string }}
  {% endtrans %}
&lt;/div&gt;
&lt;div&gt;
  {% trans %}
    Pass-through: {{ string|raw }}
  {% endtrans %}
&lt;/div&gt;
&lt;div&gt;
  {% trans %}
    Placeholder: {{ string|placeholder }}
  {% endtrans %}
&lt;/div&gt;


And here ist the output:

Escaped: &lt;strong&gt;&amp;amp;&lt;/strong&gt;
Pass-through: &lt;strong&gt;&amp;amp;&lt;/strong&gt;
Placeholder: &lt;strong&gt;&amp;amp;&lt;/strong&gt;


Is there any filter that will switch off escaping?
AFAIK the Twig trans tag in Drupal 8 was implemented to ease translations to themers that were already familiar with Twig.

Under the hood, it's working with the same kind of translation strings as the regular Drupal translation function t() or the Drupal Twig filter |t, whereas all variables you are using within the trans block are handled as @variable or %variable placeholder within the translation strings.

These placeholders within the translation function always escape HTML before being inserted to the translation. No matter how you try to not escape the values you pass in, except you pass in a FormattableMarkup object.

Unsanitized variables ({{ variable|raw }} in Twig templates or !variable in translation strings) are deprecated for Drupal 8 translations and won't work in latest 8.x Drupal versions. The translation API description is a little outdated.

For your use case, you may just add the HTML link tag to your translation string and make the link URL itself the variable. Unfortunately, the Twig trans tag doesn't support that kind of variable yet. (Compare this core issue.) So you should use the t filter instead:

{{ 'A &lt;a href=":url"&gt;link&lt;/a&gt; example in a translation.'|t({ ':url': url }) }}


The above example matches the translation strings:

en: A &lt;a href=":url"&gt;link&lt;/a&gt; example in a translation.

de: Ein Beispiel-&lt;a href=":url"&gt;Link&lt;/a&gt; in einer Übersetzung.