Expressions for named parameters when translating.

This is a feature request for changing how evaluation of expressions in the Yii::t() method works, but there’s also a bit about smart usage of message keys when using parameters.

The current documentation of using expressions in translations in Yii shows examples like the following.

Say I have the following code in my application.

echo Yii::t('mail', '1#You have {n} new message|n>1#You have {n} new messages', $count);

This is a reasonable approach, but what if I have several keys?

$args = array($n, '{start}' => $start ... );

Yii::t('zii','Displaying {start}-{end} of {n} result(s).', $args);

I am still able to do apply the expression trick here, but what does it actually look like if I want to present a grammatically correct text, based on a specific parameter?

$args = array($n, '{start}' => $start ... );

Yii::t('zii','1#Displaying {start}-{end} of {n} result.|n>1#Displaying {start}-{end} of {n} results.', $args);

It’s still doable, but I think most can agree with me, when I say that this isn’t exactly a clean solution. Remember that the expression logic is both in the message key, wherever it is called from, as the key of the value and most likely in the value itself. This goes against the DRY principle of software development.

I dug into the Yii::t() method, and found that I can actually do the following.

// calling code

echo Yii::t('zii', 'Displaying {start}-{end} of {n} result(s)', $args);

// messages/LOCALE/zii.php


'Displaying {start}-{end} of {n} result(s)' => '1#Displaying {start}-{end} of {n} result.|n>1#Displaying {start}-{end} of {n} results.'


I removed the expression logic from the message key and isolated it to the value itself.

I’m wondering why the Yii documentation doesn’t encourage (or inform of) this way of using the message keys. It is arguably easier to read the code from where the translation is called, there’s less duplicate typing and the trick will always work (unless YiiBase->_app evaluates to null, but that’s silly.)

In addition to this, I think supporting the named parameters in the expressions would be a great benefit in making the code more abstract, rather than always relying on the magical {n} parameter.

An example of the usage:

// calling code

$args = array(

	'start' => $start,

	'end' => $end,

	'count' => $count


echo Yii::t('zii', 'Displaying {start}-{end} of {count} result(s)', $args);

// messages/LOCALE/zii.php


'Displaying {start}-{end} of {count} result(s)' => 'count==1#Displaying {start}-{end} of {count} result.|count>1#Displaying {start}-{end} of {count} results.'


I’m all for keeping the default {n} parameter, since it makes it easy to insert quick one-line plural translations, but the support for the other parameters would make life easier, since you won’t have to worry about how you pass the parameters, just how you write the expression in the language file.

I’d also mention that I hope Yii will switch to using php’s gettext functions for internationalization files. I don’t know a whole lot about the functions, but since there are various tools for editing .po files, most people are able to edit them, while only people with knowledge of programming are able to modify the php arrays without being likely to break them. Thinking twice, this is more suited for an extension, Yii’s shining star!

Please voice your opinion, I want to know if I’m missing something.