Symfony 6 (php8) XLIFF plurals now working

382 Views Asked by At

I'm working with Symfony 6.1.1 + php8.1.7 and have a problem with translations (XLIFF)

If the translation file is without translation all fine but pluralization won't work correctly with the translation key in the file

Translation call in Twig

{{ "{0}Any item found|{1}One item found|]1,Inf[ Found %count% items" | trans({'%count%': paginated_data.totalItemCount}) }}

Result with deleted translation from .xlf enter image description here

And result with existing translation in the file (In the picture you can see, that translator gives the translation from the correct target block)

result with existing translation in file

There is part of the xliff file with translation

<trans-unit id="Y9p7sja1" resname="{0}Any item found|{1}One item found|]1,Inf[ Found %count% items">
    <source>{0}Any item found|{1}One item found|]1,Inf[ Found %count% items</source>
    <target>{0}Any item found1|{1}One item found2|]1,Inf[ Found %count% items3</target>
</trans-unit>

In debug panel, all is fine and Symfony says that pluralization working In debug panel all is fine and Symfony says that pluralization working

2

There are 2 best solutions below

0
Dmitriy.Net On BEST ANSWER

The answer much simplest than i've expected.

Symofny XLIFF format for translations doesn't support pecent "%" sign for mark variables, it means that you must to change percent to some another sign like bracers "{}" and it's going to be work.

In the previous translation format, placeholders were often wrapped in % (e.g. %name%). This % character is no longer valid with the ICU MessageFormat syntax, so you must rename your parameters if you are upgrading from the previous format.

<trans-unit id="Y9p7sja1" resname="{0}Any item found|{1}One item found|]1,Inf[ Found {count} items">
    <source>{0}Any item found|{1}One item found|]1,Inf[ Found {count} items</source>
    <target>{0}Any item found1|{1}One item found2|]1,Inf[ Found {count} items3</target>
</trans-unit>
0
Falco On

Your xlf should look like this:

<unit id="Y9p7sja1" name="plural_items">
    <segment>
        <source>plural_items</source>
        <target>{items, plural, =0 {Any item found} =1 {One item found} other {Found # items}}</target>
    </segment>
</unit>

And in Twig:

{{ 'plural_items'|trans({'items': paginated_data.totalItemCount}) }}