CFormatter parse values and be locale aware


(Rangelspam) #1

On the application class there is a ‘format’ property that can be used to format values, but there is no corresponding methods to parse values back into PHP formats.

I suggest that the CFormatter class also include parsing functions, the same as the format ones, like parseDate, parseRaw, etc, so it could be used with a common API for both functions.

Also, the CLocale class contains all information that CFormatter could use to format the values, so to not break compatibility, I suggest the creation of a CLocaleFormatter class that formats/parses values using the locale.

Attached is a patch of a implementation I made of this, on Yii trunk.

Comments are welcome.


(Mr) #2

This is an old topic, but I just stumbled upon this problem now:

When using CDetailView or CGridView, the easiest way to set the appropiate display value is by setting the ‘type’ attribute of the column. This uses CFormatter (Yii::app()->format) which to my surprise is not locale aware.

It would be good if CFormatter was locale aware, but it’s also pretty simple to change this in your application.

Open your main config and add: (example for Dutch)




'components' => array(

    (...)

        'format' => array(

		'dateFormat' => 'd-m-Y',

		'numberFormat' => array('decimals'=>null, 'decimalSeparator'=>',', 'thousandSeparator'=>'.'),

		'booleanFormat' => array('Nee', 'Ja'),

        ),

    (...)