Localized version of CFormatter

2 0
5
Viewed: 13 304 times
Version: Unknown (update)
Category: How-tos
Written by: marcovtwout marcovtwout
Updated by: marcovtwout marcovtwout
Created: Oct 26, 2012
Updated: 12 years ago

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#1)next (#3) »

The default CFormatter (accessible through Yii::app()->format) is not locale aware, unfortunately. Until it is, here is a quick and easy way to localize it, by extending the default class.

Put this file in your protected/components.

class LocalizedFormatter extends CFormatter
{
	/**
	 * Initializes this class.
	 */
	public function init()
	{
		switch (Yii::app()->language) {
			case 'nl':
				$this->dateFormat = 'd-m-Y';
				$this->timeFormat = 'H:i:s';
				$this->datetimeFormat = 'd-m-Y H:i:s';
				$this->numberFormat = array(
					'decimals'=>null,
					'decimalSeparator'=>',',
					'thousandSeparator'=>'.'
				);
				$this->booleanFormat = array('Nee', 'Ja');
				break;
			// Add more language cases according to your needs        
		}
	}
}

And set the following in your config/main.php

'components' => array(
    (...)
    'format' => array(
        'class' => 'LocalizedFormatter',
    ),
    (...)
),