n How to translate text in Twig templates in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Drupal 8 incorporates a new templating system for HTML output generation called Twig – see the Drupal 8 theme guide. Twig does not use a native PHP format, so it needs its own solutions for string translations.

Simple text translation is possible with the 't' (or 'trans') filter, while a whole {% trans %} block is supported (as defined by the Twig i18n extension) for more complex scenarios, such as strings with placeholders and singular/plural combinations.

These two do the same; they run the text through t():

Example 1 with t or trans filter:

{{ 'Hello Earth.'|trans }}
{{ 'Hello Earth.'|t }}

 

Example 2 with {% trans %}:

{% trans %}
  Submitted by {{ author.username }} on {{ node.created }}
{% endtrans %}

 

Example 3 translation with variables:

<p class="submitted">{{ "Submitted by !author on @date"|t({ '!author': author, '@date': date }) }}</p>

 

Plural Translations

Making plural translations are now also even easier, just implement a {% plural ... %} switch!

Example:

{% set count = comments|length %}
{% trans %}
  {{ count }} comment was deleted successfully.
{% plural count %}
  {{ count }} comments were deleted successfully.
{% endtrans %}

 

Values are escaped by default. The 'raw' filter can be used to skip escaping. The 'placeholder' filter can be used to form a placeholder. The default behavior is equivalent to @ in t(), while 'raw' matches ! and 'placeholder' matches %:

 

{% set string = '&"<>' %}
{% trans %}
  Escaped: {{ string }}
{% endtrans %}
{% trans %}
  Raw: {{ string|raw }}
{% endtrans %}
{% trans %}
  Placeholder: {{ string|placeholder }}
{% endtrans %}

These will translate Escaped: @stringRaw: !string and Placeholder: %string respectively.

 

{% trans %} & Twig Filters

Filtering Twig variables inside the {% trans %} tag will generally work. However, some of these filters may not work properly or at all. If you are not seeing the desired result or you are receiving fatal errors/WSOD you may need scale down what you are trying to do inside the {% trans %} tag. Create a new Twig variable outside of the tag with this filter applied:

{% set date = node.created|format_date('medium') %}
{% trans %}
  Node was created on {{ date }}.
{% endtrans %}

 

Context and Language Options

 

The t() and format_plural() functions have an $options parameter that can provide additional context or allow a specific language to be chosen for translation. To pass these options in the {% trans %} tag, use the with { ... } syntax in the opening tag:

 

{% trans with {'context': 'Long month name', 'langcode': 'hu'} %}
  May
{% endtrans %}
{% trans 'May' with {'context': 'Long month name', 'langcode': 'hu'} %}

 

This will translate May as a long month name into Hungarian specifically.

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook