Export to GitHub

yii - issue #1987

Yii::t() further plural enhansment


Posted on Jan 13, 2011 by Grumpy Panda

Right now as of Yii 1.1.6 we will be able to do plural's with Yii::t, but only simple one, like:

echo $number.' '.Yii::t('category', 'apple|apples', $number);

The first thing that came to my mind was "Hell, i'm fedup with wrinting that $numder.' '." from gettext times. Well, with gettext is was usually solved like this:

echo sprintf(ngettext('%s apple', '%s apples', $number), $numder)

Not really shorter, but at least the translation guy is told that %s means there will be replacement of that %s to number (generally it is used widely, to push in things that are generated on the fly).

So, why not take a step further:

echo Yii::t('category', '%s apple|%s apples', 10); // 10 apples or even echo Yii::t('category', 'apple|apples', 10); // 10 apples But I like the first one, because it gives ability to place the number in a position I want. For example

Yii::t('category', 'You have # message|You have # messages', 10);

The reason for this posted really is that when you translate something, it is better to view the whole phrase than a part of it. Taking the latest example, we would have 2 peaces to translate there: 'You have {number} {messages}' and 'message|messages' and quite a mess in the code:

echo Yii::t('category', 'You have {number} {messages}', array('{number}' => $number, '{messages}' => Yii::t('category', 'message|messages', 10)));

That's for the part 1, it's quite easy.

The second part is tricky and I didn't figure out yet hot it can be done without some parsing, rolling out the example:

echo Yii::t('app', 'You have [# new message|# new messages] in [# folder|# folders]', array(10, 3)); // You have 10 new messages in 3 folders

Tricky part is how to make it work with gettext. And i'm attempting to make Yii's Yii::t to make work with the native gettext, so this can be a "can't do" stuff.

Comment #1

Posted on Jan 13, 2011 by Grumpy Panda

echo Yii::t('category', '%s apple|%s apples', 10); // 10 apples

has to be

echo Yii::t('category', '# apple|# apples', 10); // 10 apples

or it can use sprintf stuff really

Comment #2

Posted on Jan 13, 2011 by Helpful Camel

(No comment was entered for this change.)

Comment #3

Posted on Jan 14, 2011 by Helpful Camel

We will do Yii::t('category', '1 apple|{n} apples', 10)

Comment #4

Posted on Jan 14, 2011 by Massive Cat

(No comment was entered for this change.)

Comment #5

Posted on Jan 14, 2011 by Massive Cat

This issue was closed by revision r2858.

Status: Fixed

Labels:
Priority-Medium Milestone-1.1.6