Localized version of CFormatter

You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#2)

  1. Update!
  2. Original article

Update!

Since Yii 1.1.14, a CLocalizedFormatter is available.

To replace the application component 'format', which is registered by CApplication by default, you can put this in your application 'components' config:

'format' => array('class' => 'CLocalizedFormatter'),

Original article

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',
    ),
    (...)
),
2 0
5 followers
Viewed: 10 493 times
Version: Unknown (update)
Category: How-tos
Written by: marcovtwout
Last updated by: marcovtwout
Created on: Oct 26, 2012
Last updated: 10 years ago
Update Article

Revisions

View all history