CChoiceFormat::format for a bit more complex format selection

Hi there,

I’m trying to use CChoiceFormat::format for a little bit more complex format selection. And I don’t know, why below presented code doesn’t work.

$rule = 'n==1#znak|(n>=2) && (n<5)#znaki|n>=5#znaków';

$transl = CChoiceFormat::format($rule, $max);

It works, but incorrectly. Looks like only rule one and three are executed - i.e. I got "znak" for 1, and "znaków for 2 to infinity. While I should get "znak" for 1, "znaki" for 2-4 and "znaków" for 5-infinity.

Can someone help me here?



I have not yet used this feature… but as the doc say

It seems that your second rule is problematic… try to play with it… like removing the parenthesis…

Yes. That’s exactly what is happening.

The problem is, that I have no idea, what can be changed. I tested version with removed parenthesis and it also did not work as expected. This rule is very easy and, on the other hand, I don’t know CChoiceFormat much. I’m running out of ideas, why this is not working or what could be changed. That’s why I asked. :]


I just now copy/pasted your code but tried with a fixed number like

$transl = CChoiceFormat::format($rule, 2);

And I’m getting the right output - “znaki”

Check what is going on with your $max variable… or if this is not the problem maybe it’s the Yii version… I’m using the latest one from the SVN trunk…

No, $max variable is 100% correct. Double checked it – not only, it comes from Yii/HTML (I’m using it to display information about max possible character per form field – i.e. $htmlOptions[‘maxlength’]), but I also print this value before CChoiceFormat result, so I have full control on it.

I agree that I’m using a rather old version of Yii in particular project, where I use CChoiceFormat. For the reasons, I don’t want to discuss here, I can’t update to the newer version (although I now that not updating is a bad idea). And I don’t have possibility to check it in another project running under newest Yii version.

So, to conclude, it is good to know, that it works like should in newest version. We could trace, when and how it was fixed, by analysing version changes and code commits, but I don’t see reason for doing so. Thanks and EoT.