{% trans %} does not support render array and MarkupInterface valued placeholders

Created on 7 September 2014, almost 11 years ago
Updated 18 April 2025, 2 months ago

Problem/Motivation

Noticed in #2226493: Apply formatters and widgets to Node base fields β†’ . Right now you get warnings and errors.

Right now {% trans %} only allows strings to be passed in and using render arrays is not possible. Being able to use render arrays in node.html.twig would allow us to simplify template_preprocess_node().

Proposed resolution

Change this line:
$compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', ');
in TwigNodeTrans.php

API changes

Allows common values to be rendered in Trans tag.

Before

After

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡¨πŸ‡­Switzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

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.

  • First commit to issue fork.
  • Rebased MR against 11.x, and h/t alexpott for changing MR target branch.

    Changed to use an inline template test instead. All tests passing now.

  • Per note from alexpott on Slack, pushed commits to clean up some todos around this issue.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    So tested this by kinda following the CR (which updated the version numbers)

    Copied
    $variables['render_array'] = [
    '#prefix' => '',
    ];

    Into olivero_preprocess_page_title and placed

    {% trans %}
    This is a {{ render_array }}.
    {% endtrans %}

    In the page_title.html.twig file.

    Got a fatal error

    Applied the MR

    Now the error is gone and "This is a trans render array" appears just fine.

  • First commit to issue fork.
  • Status changed to RTBC 13 days ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Updating credit

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I read the comments, the MR and the CR. I didn't seen any unanswered questions. I don't see a review of the change record and I made an edit anyway so I am tagging for that for review.. There is one question in the MR.

  • Addressed MR comment. I also removed custom assert messages per 🌱 [META] [policy] Remove PHPUnit assertion messages when possible, and standardize remaining messages Active in the tests.

    Also updated the CR and the issue title, because I think the word "placeholder" is confusing. Not sure exactly what Twig's terminology is for {{ ... }} syntax, but from the docs, there's this:

    There are two kinds of delimiters: {% ... %} and {{ ... }}. The first one is used to execute statements such as for-loops, the latter outputs the result of an expression.

    I've updated language in this issue title to replace "placeholder" with "expression" or "rendered expression" for lack of better terminology.

Production build 0.71.5 2024