CLocalizedFormatter.php 4.12 KB
Newer Older
JULIO JARAMILLO's avatar
JULIO JARAMILLO committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<?php
/**
 * CLocalizedFormatter class file.
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @link http://www.yiiframework.com/
 * @copyright 2008-2013 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

/**
 * CLocalizedFormatter provides a set of commonly used data formatting methods based on the current locale settings.
 *
 * It provides the same functionality as {@link CFormatter}, but overrides all the settings for
 * {@link booleanFormat}, {@link datetimeFormat} and {@link numberFormat} with the values for the
 * current locale. Because of this you are not able to configure these properties for CLocalizedFormatter directly.
 * Date and time format can be adjusted by setting {@link dateFormat} and {@link timeFormat}.
 *
 * It uses {@link CApplication::locale} by default but you can set a custom locale by using {@link setLocale}-method.
 *
 * For a list of recognizable format types, and details on how to call the formatter methods,
 * see {@link CFormatter} documentation.
 *
 * To replace the application component 'format', which is registered by {@link CApplication} by default, you can
 * put this in your application 'components' config:
 * <code>
 * 'format' => array(
 *     'class' => 'CLocalizedFormatter',
 * ),
 * </code>
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @package system.utils
 * @since 1.1.14
 */
class CLocalizedFormatter extends CFormatter
{
	private $_locale;
	/**
	 * @var string the width of the date pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.
	 * @see CDateFormatter::formatDateTime()
	 */
	public $dateFormat='medium';
	/**
	 * @var string the width of the time pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.
	 * @see CDateFormatter::formatDateTime()
	 */
	public $timeFormat='medium';

	/**
	 * Set the locale to use for formatting values.
	 * @param CLocale|string $locale an instance of CLocale or a locale ID
	 */
	public function setLocale($locale)
	{
		if(is_string($locale))
			$locale=CLocale::getInstance($locale);
		$this->sizeFormat['decimalSeparator']=$locale->getNumberSymbol('decimal');
		$this->_locale=$locale;
	}

	/**
	 * @return CLocale $locale the locale currently used for formatting values
	 */
	public function getLocale()
	{
		if($this->_locale === null) {
			$this->setLocale(Yii::app()->locale);
		}
		return $this->_locale;
	}

	/**
	 * Formats the value as a boolean.
	 * @param mixed $value the value to be formatted
	 * @return string the formatted result
	 * @see booleanFormat
	 */
	public function formatBoolean($value)
	{
		return $value ? Yii::t('yii','Yes') : Yii::t('yii','No');
	}

	/**
	 * Formats the value as a date using the {@link locale}s date formatter.
	 * @param mixed $value the value to be formatted
	 * @return string the formatted result
	 * @see dateFormat
	 * @see CLocale::getDateFormatter()
	 */
	public function formatDate($value)
	{
		return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormat, null);
	}

	/**
	 * Formats the value as a time using the {@link locale}s date formatter.
	 * @param mixed $value the value to be formatted
	 * @return string the formatted result
	 * @see timeFormat
	 * @see CLocale::getDateFormatter()
	 */
	public function formatTime($value)
	{
		return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), null, $this->timeFormat);
	}

	/**
	 * Formats the value as a date and time using the {@link locale}s date formatter.
	 * @param mixed $value the value to be formatted
	 * @return string the formatted result
	 * @see dateFormat
	 * @see timeFormat
	 * @see CLocale::getDateFormatter()
	 */
	public function formatDatetime($value)
	{
		return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormat, $this->timeFormat);
	}

	/**
	 * Formats the value as a number using the {@link locale}s number formatter.
	 * @param mixed $value the value to be formatted
	 * @return string the formatted result
	 * @see CLocale::getNumberFormatter()
	 */
	public function formatNumber($value)
	{
		return $this->getLocale()->numberFormatter->formatDecimal($value);
	}
}