CLocalizedFormatter.php 4.1 KB

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