Skip to content

[TwigComponent] Twig PreLexer gets confused by ICU MessageFormat plural syntax #3514

Description

@balbinus

I have run into an edge case where I’m using ICU messages in a translatable string, and the message has plural cases in it. See here for a more complete documentation, but here’s an example string:

{count, plural, 
   one {# item}
   other {# items}
}

This trips up the Twig PreLexer and makes it look for an end-of-comment #} that it may or may not find, and ignore all <twig:Blocks> until it finds one.

Here's a minimal template as example:

There are {{ '{count, plural, one {# item} other {# items}}'|trans({'count': 42}) }}!

<twig:Test />

It outputs:

There are {42, plural, one {# item} other {# items}}!

<twig:Test />#}

The ICU syntax isn’t processed in the raw string, but this doesn’t matter in practice since it’s only here as a placeholder for translators. Real strings from XLIFF files are handled correctly. Most importantly, the HTML tag is present in the output… Now, if I modify the template to add a “fake” comment:

There are {{ '{count, plural, one {# item} other {# items}}'|trans({'count': 42}) }}!
{# Comment #}
<twig:Test />

The component is now rendered:

There are {42, plural, one {# item} other {# items}}!
TEST COMPONENT

I understand this is niche/edge case, and in any case thanks a lot for your hard work on this project and thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions