ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Intl.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 2010 Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 {
14  public function __construct()
15  {
16  if (!class_exists('IntlDateFormatter')) {
17  throw new RuntimeException('The intl extension is needed to use intl-based filters.');
18  }
19  }
20 
24  public function getFilters()
25  {
26  return array(
27  new Twig_SimpleFilter('localizeddate', 'twig_localized_date_filter', array('needs_environment' => true)),
28  new Twig_SimpleFilter('localizednumber', 'twig_localized_number_filter'),
29  new Twig_SimpleFilter('localizedcurrency', 'twig_localized_currency_filter'),
30  );
31  }
32 
36  public function getName()
37  {
38  return 'intl';
39  }
40 }
41 
42 function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian')
43 {
44  $date = twig_date_converter($env, $date, $timezone);
45 
46  $formatValues = array(
47  'none' => IntlDateFormatter::NONE,
48  'short' => IntlDateFormatter::SHORT,
49  'medium' => IntlDateFormatter::MEDIUM,
50  'long' => IntlDateFormatter::LONG,
51  'full' => IntlDateFormatter::FULL,
52  );
53 
54  $formatter = IntlDateFormatter::create(
55  $locale,
56  $formatValues[$dateFormat],
57  $formatValues[$timeFormat],
58  PHP_VERSION_ID >= 50500 ? $date->getTimezone() : $date->getTimezone()->getName(),
59  'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL,
60  $format
61  );
62 
63  return $formatter->format($date->getTimestamp());
64 }
65 
66 function twig_localized_number_filter($number, $style = 'decimal', $type = 'default', $locale = null)
67 {
68  static $typeValues = array(
69  'default' => NumberFormatter::TYPE_DEFAULT,
70  'int32' => NumberFormatter::TYPE_INT32,
71  'int64' => NumberFormatter::TYPE_INT64,
72  'double' => NumberFormatter::TYPE_DOUBLE,
73  'currency' => NumberFormatter::TYPE_CURRENCY,
74  );
75 
76  $formatter = twig_get_number_formatter($locale, $style);
77 
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 
82  return $formatter->format($number, $typeValues[$type]);
83 }
84 
85 function twig_localized_currency_filter($number, $currency = null, $locale = null)
86 {
87  $formatter = twig_get_number_formatter($locale, 'currency');
88 
89  return $formatter->formatCurrency($number, $currency);
90 }
91 
101 {
102  static $formatter, $currentStyle;
103 
104  $locale = $locale !== null ? $locale : Locale::getDefault();
105 
106  if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
107  // Return same instance of NumberFormatter if parameters are the same
108  // to those in previous call
109  return $formatter;
110  }
111 
112  static $styleValues = array(
113  'decimal' => NumberFormatter::DECIMAL,
114  'currency' => NumberFormatter::CURRENCY,
115  'percent' => NumberFormatter::PERCENT,
116  'scientific' => NumberFormatter::SCIENTIFIC,
117  'spellout' => NumberFormatter::SPELLOUT,
118  'ordinal' => NumberFormatter::ORDINAL,
119  'duration' => NumberFormatter::DURATION,
120  );
121 
122  if (!isset($styleValues[$style])) {
123  throw new Twig_Error_Syntax(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
124  }
125 
126  $currentStyle = $style;
127 
128  $formatter = NumberFormatter::create($locale, $styleValues[$style]);
129 
130  return $formatter;
131 }
132 
133 class_alias('Twig_Extensions_Extension_Intl', 'Twig\Extensions\IntlExtension', false);
$style
Definition: example_012.php:70
$format
Definition: metadata.php:141
$type
twig_date_converter(Twig_Environment $env, $date=null, $timezone=null)
Converts an input to a DateTime instance.
Definition: Core.php:422
Represents a template filter.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
getFilters()
{Returns a list of filters to add to the existing list.Twig_SimpleFilter[]}
Definition: Intl.php:24
$env
twig_get_number_formatter($locale, $style)
Gets a number formatter instance according to given locale and formatter.
Definition: Intl.php:100
twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat='medium', $timeFormat='medium', $locale=null, $timezone=null, $format=null, $calendar='gregorian')
Definition: Intl.php:42
Create styles array
The data for the language used.
twig_localized_currency_filter($number, $currency=null, $locale=null)
Definition: Intl.php:85
getName()
{Returns the name of the extension.string The extension namesince 1.26 (to be removed in 2...
Definition: Intl.php:36
twig_localized_number_filter($number, $style='decimal', $type='default', $locale=null)
Definition: Intl.php:66
Stores the Twig configuration.
Definition: Environment.php:17