1: <?php
2: /**
3: * CLocalizedFormatter class file.
4: *
5: * @author Carsten Brandt <mail@cebe.cc>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CLocalizedFormatter provides a set of commonly used data formatting methods based on the current locale settings.
13: *
14: * It provides the same functionality as {@link CFormatter}, but overrides all the settings for
15: * {@link booleanFormat}, {@link datetimeFormat} and {@link numberFormat} with the values for the
16: * current locale. Because of this you are not able to configure these properties for CLocalizedFormatter directly.
17: * Date and time format can be adjusted by setting {@link dateFormat} and {@link timeFormat}.
18: *
19: * It uses {@link CApplication::locale} by default but you can set a custom locale by using {@link setLocale}-method.
20: *
21: * For a list of recognizable format types, and details on how to call the formatter methods,
22: * see {@link CFormatter} documentation.
23: *
24: * To replace the application component 'format', which is registered by {@link CApplication} by default, you can
25: * put this in your application 'components' config:
26: * <code>
27: * 'format' => array(
28: * 'class' => 'CLocalizedFormatter',
29: * ),
30: * </code>
31: *
32: * @author Carsten Brandt <mail@cebe.cc>
33: * @package system.utils
34: * @since 1.1.14
35: */
36: class CLocalizedFormatter extends CFormatter
37: {
38: private $_locale;
39: /**
40: * @var string the width of the date pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.
41: * @see CDateFormatter::formatDateTime()
42: */
43: public $dateFormat='medium';
44: /**
45: * @var string the width of the time pattern. It can be 'full', 'long', 'medium' and 'short'. Defaults to 'medium'.
46: * @see CDateFormatter::formatDateTime()
47: */
48: public $timeFormat='medium';
49:
50: /**
51: * Set the locale to use for formatting values.
52: * @param CLocale|string $locale an instance of CLocale or a locale ID
53: */
54: public function setLocale($locale)
55: {
56: if(is_string($locale))
57: $locale=CLocale::getInstance($locale);
58: $this->sizeFormat['decimalSeparator']=$locale->getNumberSymbol('decimal');
59: $this->_locale=$locale;
60: }
61:
62: /**
63: * @return CLocale $locale the locale currently used for formatting values
64: */
65: public function getLocale()
66: {
67: if($this->_locale === null) {
68: $this->setLocale(Yii::app()->locale);
69: }
70: return $this->_locale;
71: }
72:
73: /**
74: * Formats the value as a boolean.
75: * @param mixed $value the value to be formatted
76: * @return string the formatted result
77: * @see booleanFormat
78: */
79: public function formatBoolean($value)
80: {
81: return $value ? Yii::t('yii','Yes') : Yii::t('yii','No');
82: }
83:
84: /**
85: * Formats the value as a date using the {@link locale}s date formatter.
86: * @param mixed $value the value to be formatted
87: * @return string the formatted result
88: * @see dateFormat
89: * @see CLocale::getDateFormatter()
90: */
91: public function formatDate($value)
92: {
93: return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormat, null);
94: }
95:
96: /**
97: * Formats the value as a time using the {@link locale}s date formatter.
98: * @param mixed $value the value to be formatted
99: * @return string the formatted result
100: * @see timeFormat
101: * @see CLocale::getDateFormatter()
102: */
103: public function formatTime($value)
104: {
105: return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), null, $this->timeFormat);
106: }
107:
108: /**
109: * Formats the value as a date and time using the {@link locale}s date formatter.
110: * @param mixed $value the value to be formatted
111: * @return string the formatted result
112: * @see dateFormat
113: * @see timeFormat
114: * @see CLocale::getDateFormatter()
115: */
116: public function formatDatetime($value)
117: {
118: return $this->getLocale()->dateFormatter->formatDateTime($this->normalizeDateValue($value), $this->dateFormat, $this->timeFormat);
119: }
120:
121: /**
122: * Formats the value as a number using the {@link locale}s number formatter.
123: * @param mixed $value the value to be formatted
124: * @return string the formatted result
125: * @see CLocale::getNumberFormatter()
126: */
127: public function formatNumber($value)
128: {
129: return $this->getLocale()->numberFormatter->formatDecimal($value);
130: }
131: }
132: