Symfony transchoice can't choose a translation

2019-08-30 07:15发布

问题:

'permission.label'|transchoice(0, {}, 'someBundle')

With above code I'm getting the following error when enabling "sl" as locale:

Unable to choose a translation for "Dovoljenje|Dovoljenja" with locale "sl" for value "0". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %count% apples").

In other languages (english, german, french, italian, +10 more) I don't have any issues with this. Only slovenian breaks my complete application bc transchoice is unable to pick a translation.

For example "Permission|Permissions" works fine with locale "en" and value "0". But in "sl" it will throw the above error.

Any idea what is going on here?

Thank you!

回答1:

Your string "Dovoljenje|Dovoljenja" only has two choices like many languages that use the forms "one" and "other".

Slovenian has more forms than that, which is why your other languages work, but Slovenian doesn't.

Looking at the plural rules baked into the Translation component, the equation for Slovenian is as follows:

(1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : 
(((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3));

This indicates that there are four forms: "one" "two" "few" and "other". Substituting $number=0 will return the final "other"" option as offset [3]which does not exist in your string.

I don't speak Slovenian, but if "two" "few" and "other" can all have the same translation then set your string to "Dovoljenje|Dovoljenja|Dovoljenja|Dovoljenja" and the error will go away.