regexp support in message choice format

This a very small issue, but also very easy to implement, and I think it would make Yii’s translation system adaptable to the strangest language rule.

Take the message “{username}'s friends”. A second version of this message is needed for when {username} ends in letter s (should be “{username}’ friends” in this case). Similar quirks exist in languages like French, Italian or Catalan: de vs. d’, la vs. l’, etc.

What I propose is to add support for regular expressions inside choice format. In my example, I’d add the following entry to the messages file:




"{username}'s friends" => "/s$/i#{username}' friends|#{username}'s friends",



That is, the first message would be used if the choice param matches the regexp "/s$/i", and the second message otherwise.

In the views I’d make the call:




echo Yii::t('app', "{username}'s friends", array($username, '{username}'=>$username));



This can be achieved by adding a new condition to CChoiceFormat:




//integers

if($expression==="$intval")

{

  if($intval==$number)

    return $message;

}

//regular expressions

else if (strpos($expression, '/')===0)

{

  if (preg_match($expression, $number))

    return $message;

}

//rest of expressions

else if(self::evaluate(str_replace('n','$n',$expression),$number))

  return $message;



Not sure about performance penalty but this will add a great flexibility.