Somewhere between twig 3.20 and 3.21.1 pipes have been changed from PUNCTUATION_TYPE to OPERATOR_TYPE so if you have strings translated like {{ 'test'|t }} potx won't detect them.

Attached patch fixes this for us, but there should probably be a more robust fix that works for both twig versions.

Issue fork potx-3528177

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jvollebregt-swis created an issue. See original summary.

jvollebregt-swis’s picture

StatusFileSize
new1.38 KB

Sorry about that, accidentally made the patch on the 7.x branch

casey’s picture

The bug is introduced since twig 3.21.0:

Extract operators logic from ExpressionParser to their own classes
https://github.com/twigphp/Twig/commit/42c82e1e9b88af365b15ed35cb5488f16...

https://github.com/twigphp/Twig/blob/3.x/CHANGELOG:

# 3.21.0 (2025-05-02)

...
 * Deprecate using the `|` operator in an expression with `+` or `-` without using parentheses to clarify precedence
 * Deprecate operator precedence outside of the [0, 512] range
 * Introduce expression parser classes to describe operators and operands provided by extensions
...
volman’s picture

Title: Potx doesn't recognize t filters in twig 3.21 » Potx doesn't recognize t filters and dots in variables as of twig 3.21
StatusFileSize
new1.96 KB

Twig 3.21.0 not only broke extracting from |t filter. Potx now incorrectly extracts variable names containing a dot ({{ node.type }} becomes @node instead of @node.type).

This patch expands on #2. It fixes both issues and should work with earlier twig versions as well.

gábor hojtsy’s picture

Status: Active » Needs review

Can you put this into an MR? That way tests would run on it too :)

jvollebregt-swis’s picture

I've put my original patch into an MR. @volman do you have push access to my branch? If not do you want me to add your patch to the branch?

johnatas’s picture

Hello,

I confirm that @volman’s patch #4 works.
Tested on Drupal 11.2.2 and 11.2.3 with potx 1.1.0.

@volman, as mentioned in the previous comment, could you please push the patch to the branch?

Thank you.

volman’s picture

#4 patch was pushed to MR !33 and the tests are passing.

johnatas’s picture

Thanks @volman.

Everything looks fine to me.

@jvollebregt-swis, does it look good to you as well?

ricovandevin’s picture

We can confirm the issue and that this issue is fixed when applying the provided patch.

johnatas’s picture

Status: Needs review » Reviewed & tested by the community
watergate’s picture

Although already RTBC, I can also confirm the patch (i.e., MR) fixes the issue.

espurnes’s picture

Hello,
The patch solves the translation detection on twigs, but it seems it does not respect the context.
I've tried to export translations in twig files like this:
{{ 'my text'|t({}, {'context': 'my context' }}

The translation is exported without context.

Before d11, I was using potx applying the following patch: https://www.drupal.org/project/potx/issues/2816601#comment-14971928
but it no longer applies because that patch modifies some of the lines modified here.

Anyone has faced the context translation issue?

alexpott changed the visibility of the branch 3528177-potx-doesnt-recognize to hidden.

alexpott’s picture

This looks good - it fixes the test and preserves compatibility with older versions of Twig. I've also backported the fix to the 7.x branch so that it is ready if we update twig there.

alexpott’s picture

We do have test coverage that is now failing on 8.x due to this - https://git.drupalcode.org/issue/potx-3553345/-/jobs/6961106.

7.x does not fail because we're using an older twig version there.

alexpott’s picture

#14 is an existing bug - see #1299966-27: String context not taken into account when retrieving a translation - this issue does not fix it or break it. I've created #3553786: Extract context from t or trans filter in Twig template to track it and hopefully someone will fix it.

penyaskito made their first commit to this issue’s fork.

penyaskito’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • penyaskito committed e0a71e54 on 7.x-3.x authored by alexpott
    [#3528177] fix(Twig): Potx doesn't recognize t filters and dots in...

ressa’s picture

Thanks for maintaining this great module!

To avoid accidentally making a patch for the wrong branch (which I have done myself countless times) is it possible to set the default GitLab branch to current Drupal (8.x), and not Drupal 7?

alexpott’s picture

@ressa this one is a bit complex localize.drupal.org is still using the Drupal 7 module so it is vital that all work happens on that branch.

ressa’s picture

Thanks @alexpott, it did cross my mind that something like that might be a factor here ... so thanks for clarifying.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.