Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Intl.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 intl extension is needed to use intl-based filters.');
  16. }
  17. }
  18. /**
  19. * Returns a list of filters to add to the existing list.
  20. *
  21. * @return array An array of filters
  22. */
  23. public function getFilters()
  24. {
  25. return array(
  26. new Twig_SimpleFilter('localizeddate', 'twig_localized_date_filter', array('needs_environment' => true)),
  27. new Twig_SimpleFilter('localizednumber', 'twig_localized_number_filter'),
  28. new Twig_SimpleFilter('localizedcurrency', 'twig_localized_currency_filter'),
  29. );
  30. }
  31. /**
  32. * Returns the name of the extension.
  33. *
  34. * @return string The extension name
  35. */
  36. public function getName()
  37. {
  38. return 'intl';
  39. }
  40. }
  41. function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null)
  42. {
  43. $date = twig_date_converter($env, $date, $timezone);
  44. $formatValues = array(
  45. 'none' => IntlDateFormatter::NONE,
  46. 'short' => IntlDateFormatter::SHORT,
  47. 'medium' => IntlDateFormatter::MEDIUM,
  48. 'long' => IntlDateFormatter::LONG,
  49. 'full' => IntlDateFormatter::FULL,
  50. );
  51. $formatter = IntlDateFormatter::create(
  52. $locale,
  53. $formatValues[$dateFormat],
  54. $formatValues[$timeFormat],
  55. $date->getTimezone()->getName(),
  56. IntlDateFormatter::GREGORIAN,
  57. $format
  58. );
  59. return $formatter->format($date->getTimestamp());
  60. }
  61. function twig_localized_number_filter($number, $style = 'decimal', $type = 'default', $locale = null)
  62. {
  63. static $typeValues = array(
  64. 'default' => NumberFormatter::TYPE_DEFAULT,
  65. 'int32' => NumberFormatter::TYPE_INT32,
  66. 'int64' => NumberFormatter::TYPE_INT64,
  67. 'double' => NumberFormatter::TYPE_DOUBLE,
  68. 'currency' => NumberFormatter::TYPE_CURRENCY,
  69. );
  70. $formatter = twig_get_number_formatter($locale, $style);
  71. if (!isset($typeValues[$type])) {
  72. throw new Twig_Error_Syntax(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
  73. }
  74. return $formatter->format($number, $typeValues[$type]);
  75. }
  76. function twig_localized_currency_filter($number, $currency = null, $locale = null)
  77. {
  78. $formatter = twig_get_number_formatter($locale, 'currency');
  79. return $formatter->formatCurrency($number, $currency);
  80. }
  81. /**
  82. * Gets a number formatter instance according to given locale and formatter.
  83. *
  84. * @param string $locale Locale in which the number would be formatted
  85. * @param int $style Style of the formatting
  86. *
  87. * @return NumberFormatter A NumberFormatter instance
  88. */
  89. function twig_get_number_formatter($locale, $style)
  90. {
  91. static $formatter, $currentStyle;
  92. $locale = $locale !== null ? $locale : Locale::getDefault();
  93. if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
  94. // Return same instance of NumberFormatter if parameters are the same
  95. // to those in previous call
  96. return $formatter;
  97. }
  98. static $styleValues = array(
  99. 'decimal' => NumberFormatter::DECIMAL,
  100. 'currency' => NumberFormatter::CURRENCY,
  101. 'percent' => NumberFormatter::PERCENT,
  102. 'scientific' => NumberFormatter::SCIENTIFIC,
  103. 'spellout' => NumberFormatter::SPELLOUT,
  104. 'ordinal' => NumberFormatter::ORDINAL,
  105. 'duration' => NumberFormatter::DURATION,
  106. );
  107. if (!isset($styleValues[$style])) {
  108. throw new Twig_Error_Syntax(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
  109. }
  110. $currentStyle = $style;
  111. $formatter = NumberFormatter::create($locale, $styleValues[$style]);
  112. return $formatter;
  113. }