Twig error using {{content.field_name.0 |t}}

Created on 23 November 2018, over 6 years ago
Updated 31 May 2025, 8 days ago

Hi,

I activated Configuration Translation, Content Translation and Interface Translation modules and I have some twig block templates in a module that I'm customizing and using "|t" to translate the forms, but if I try to use, for example:

<h2>{{ content.field_name |t}}</h2>

or

<h2>{{ content.field_name.0 |t}}</h2>

I get the following error:

The website encountered an unexpected error. Please try again later.InvalidArgumentException: $string ("") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).

I understand that this error is caused because if I put the form value like above, Drupal generates an array and not a raw string, if I do not use "|t", {{content.field_name.0}} works fine.

The only way to not get the error is putting like this:

<h2>{{ content.field_name.0['#context'].value |t}}</h2>

There is some problem if I put like this? Because I didn't find an official orientation in Drupal twig documentation how is the best way to do this.

🐛 Bug report
Status

Closed: works as designed

Version

10.1

Component

language.module

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇰Denmark ressa Copenhagen

    I ran into this error when trying to translate as much as possible via .po files, by exposing strings as translatable Twig elements. But suffix wasn't translatable in a Bootstrap5 based theme (bootstrap5/templates/form/form-element.html.twig):

    With <span class="field-suffix">{{ suffix|trans }}</span> I got:

    The website encountered an unexpected error. Try again later.
    InvalidArgumentException: $string (" kr. per m²") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php). t() (Line: 100) __TwigTemplate_d423ca3a82915c4d447ecaf81034

    On the other hand, this works fine for Block titles (bootstrap5/templates/block/block.html.twig) and Field names (bootstrap5/templates/field/field.html.twig):

    Blocks:
    <h2{{ title_attributes }}>{{ label|trans }}</h2>

    Fields:
    <div{{ title_attributes.addClass(title_classes) }}>{{ label|trans }}</div>

  • 🇩🇰Denmark ressa Copenhagen

    I just remembered the two Twig contrib modules, and there was a solution for separating a field's content, suffix and prefix values.

  • 🇩🇰Denmark ressa Copenhagen

    I got it to work for field labels as well, by rendering the title value in /claro/templates/form-element-label.html.twig twice, before translating it:

    <label{{ attributes.addClass(classes) }}>{{ title|render|render|trans }}</label>

    This trick is also mentioned on the doc page:

    You may have to render an item before you filter it:
    {{ item|render|filter }}

    https://www.drupal.org/docs/develop/theming-drupal/twig-in-drupal/filter...

Production build 0.71.5 2024