Intl.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Extensions_Extension_Intl extends Twig_Extension
  11. {
  12. public function __construct()
  13. {
  14. if (!class_exists('IntlDateFormatter')) {
  15. throw new RuntimeException('The native PHP intl extension (http://php.net/manual/en/book.intl.php) is needed to use intl-based filters.');
  16. }
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getFilters()
  22. {
  23. return array(
  24. new Twig_SimpleFilter('localizeddate', 'twig_localized_date_filter', array('needs_environment' => true)),
  25. new Twig_SimpleFilter('localizednumber', 'twig_localized_number_filter'),
  26. new Twig_SimpleFilter('localizedcurrency', 'twig_localized_currency_filter'),
  27. );
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getName()
  33. {
  34. return 'intl';
  35. }
  36. }
  37. function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian')
  38. {
  39. $date = twig_date_converter($env, $date, $timezone);
  40. $formatValues = array(
  41. 'none' => IntlDateFormatter::NONE,
  42. 'short' => IntlDateFormatter::SHORT,
  43. 'medium' => IntlDateFormatter::MEDIUM,
  44. 'long' => IntlDateFormatter::LONG,
  45. 'full' => IntlDateFormatter::FULL,
  46. );
  47. if (PHP_VERSION_ID < 50500 || !class_exists('IntlTimeZone')) {
  48. $formatter = IntlDateFormatter::create(
  49. $locale,
  50. $formatValues[$dateFormat],
  51. $formatValues[$timeFormat],
  52. $date->getTimezone()->getName(),
  53. 'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL,
  54. $format
  55. );
  56. return $formatter->format($date->getTimestamp());
  57. }
  58. $formatter = IntlDateFormatter::create(
  59. $locale,
  60. $formatValues[$dateFormat],
  61. $formatValues[$timeFormat],
  62. IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
  63. 'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL,
  64. $format
  65. );
  66. return $formatter->format($date->getTimestamp());
  67. }
  68. function twig_localized_number_filter($number, $style = 'decimal', $type = 'default', $locale = null)
  69. {
  70. static $typeValues = array(
  71. 'default' => NumberFormatter::TYPE_DEFAULT,
  72. 'int32' => NumberFormatter::TYPE_INT32,
  73. 'int64' => NumberFormatter::TYPE_INT64,
  74. 'double' => NumberFormatter::TYPE_DOUBLE,
  75. 'currency' => NumberFormatter::TYPE_CURRENCY,
  76. );
  77. $formatter = twig_get_number_formatter($locale, $style);
  78. if (!isset($typeValues[$type])) {
  79. throw new Twig_Error_Syntax(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
  80. }
  81. return $formatter->format($number, $typeValues[$type]);
  82. }
  83. function twig_localized_currency_filter($number, $currency = null, $locale = null)
  84. {
  85. $formatter = twig_get_number_formatter($locale, 'currency');
  86. return $formatter->formatCurrency($number, $currency);
  87. }
  88. /**
  89. * Gets a number formatter instance according to given locale and formatter.
  90. *
  91. * @param string $locale Locale in which the number would be formatted
  92. * @param int $style Style of the formatting
  93. *
  94. * @return NumberFormatter A NumberFormatter instance
  95. */
  96. function twig_get_number_formatter($locale, $style)
  97. {
  98. static $formatter, $currentStyle;
  99. $locale = null !== $locale ? $locale : Locale::getDefault();
  100. if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
  101. // Return same instance of NumberFormatter if parameters are the same
  102. // to those in previous call
  103. return $formatter;
  104. }
  105. static $styleValues = array(
  106. 'decimal' => NumberFormatter::DECIMAL,
  107. 'currency' => NumberFormatter::CURRENCY,
  108. 'percent' => NumberFormatter::PERCENT,
  109. 'scientific' => NumberFormatter::SCIENTIFIC,
  110. 'spellout' => NumberFormatter::SPELLOUT,
  111. 'ordinal' => NumberFormatter::ORDINAL,
  112. 'duration' => NumberFormatter::DURATION,
  113. );
  114. if (!isset($styleValues[$style])) {
  115. throw new Twig_Error_Syntax(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
  116. }
  117. $currentStyle = $style;
  118. $formatter = NumberFormatter::create($locale, $styleValues[$style]);
  119. return $formatter;
  120. }
  121. class_alias('Twig_Extensions_Extension_Intl', 'Twig\Extensions\IntlExtension', false);