1

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

Dmitriy.Net
  • 1,476
  • 13
  • 24
  • Are you using the ICU translator or the regular one? How's the file named? – msg Jun 30 '22 at 20:17
  • messages+intl-icu.en.xlf - its file autogenerated by symfony. And as i can write in question - all works with translations, problem only with plural from translations – Dmitriy.Net Jul 01 '22 at 07:00
  • Then, you are using the wrong message format, since its named `+intl-icu` you are supposed to [use the ICU format](https://symfony.com/doc/current/translation/message_format.html#pluralization). – msg Jul 01 '22 at 10:34
  • File name is correct because if its not any of my translation would't work. But all translations except plurals are working from my file. And as I wrote upper, the filename generated by symfony and I dont renamed it – Dmitriy.Net Jul 01 '22 at 13:37
  • Yes, the file name is correct _if you are using the ICU format_, which you are, because it's the name what determines which one is used. See [here](https://symfony.com/doc/current/translation/message_format.html#using-the-icu-message-format). And as you can [see here](https://symfony.com/doc/current/translation.html#message-format), the tip box states: "If you **don't** use the ICU format...". Either change the message format or remove the `+intl-icu` from the file name to go back to the old format. – msg Jul 01 '22 at 14:04

2 Answers2

0

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>
Dmitriy.Net
  • 1,476
  • 13
  • 24
-1

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}) }}
Falco
  • 87
  • 1
  • 11