This repository was archived by the owner on Nov 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathIntlExtension.php
More file actions
143 lines (119 loc) · 4.91 KB
/
IntlExtension.php
File metadata and controls
143 lines (119 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
* This file is part of Twig.
*
* (c) 2010-2019 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extensions
{
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class IntlExtension extends AbstractExtension
{
public function __construct()
{
if (!class_exists(\IntlDateFormatter::class)) {
throw new RuntimeException('The native PHP intl extension (http://php.net/manual/en/book.intl.php) is needed to use intl-based filters.');
}
}
public function getFilters()
{
return [
new TwigFilter('localizeddate', 'twig_localized_date_filter', ['needs_environment' => true]),
new TwigFilter('localizednumber', 'twig_localized_number_filter'),
new TwigFilter('localizedcurrency', 'twig_localized_currency_filter'),
];
}
}
}
namespace
{
use Twig\Environment;
use Twig\Error\SyntaxError;
function twig_localized_date_filter(Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian')
{
$date = \twig_date_converter($env, $date, $timezone);
$formatValues = [
'none' => \IntlDateFormatter::NONE,
'short' => \IntlDateFormatter::SHORT,
'medium' => \IntlDateFormatter::MEDIUM,
'long' => \IntlDateFormatter::LONG,
'full' => \IntlDateFormatter::FULL,
];
if (!class_exists('IntlTimeZone')) {
$formatter = \IntlDateFormatter::create(
$locale,
$formatValues[$dateFormat],
$formatValues[$timeFormat],
$date->getTimezone()->getName(),
'gregorian' === $calendar ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL,
$format
);
return $formatter->format($date->getTimestamp());
}
$formatter = \IntlDateFormatter::create(
$locale,
$formatValues[$dateFormat],
$formatValues[$timeFormat],
\IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
'gregorian' === $calendar ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL,
$format
);
return $formatter->format($date->getTimestamp());
}
function twig_localized_number_filter($number, $style = 'decimal', $type = 'default', $locale = null)
{
static $typeValues = [
'default' => \NumberFormatter::TYPE_DEFAULT,
'int32' => \NumberFormatter::TYPE_INT32,
'int64' => \NumberFormatter::TYPE_INT64,
'double' => \NumberFormatter::TYPE_DOUBLE,
'currency' => \NumberFormatter::TYPE_CURRENCY,
];
$formatter = twig_get_number_formatter($locale, $style);
if (!isset($typeValues[$type])) {
throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
}
return $formatter->format($number, $typeValues[$type]);
}
function twig_localized_currency_filter($number, $currency = null, $locale = null)
{
$formatter = twig_get_number_formatter($locale, 'currency');
return $formatter->formatCurrency($number, $currency);
}
/**
* Gets a number formatter instance according to given locale and formatter.
*
* @param string $locale Locale in which the number would be formatted
* @param string $style Style of the formatting
*
* @return \NumberFormatter A NumberFormatter instance
*/
function twig_get_number_formatter($locale, $style)
{
static $formatter, $currentStyle;
$locale = null !== $locale ? $locale : \Locale::getDefault();
if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
// Return same instance of NumberFormatter if parameters are the same
// to those in previous call
return $formatter;
}
static $styleValues = [
'decimal' => \NumberFormatter::DECIMAL,
'currency' => \NumberFormatter::CURRENCY,
'percent' => \NumberFormatter::PERCENT,
'scientific' => \NumberFormatter::SCIENTIFIC,
'spellout' => \NumberFormatter::SPELLOUT,
'ordinal' => \NumberFormatter::ORDINAL,
'duration' => \NumberFormatter::DURATION,
];
if (!isset($styleValues[$style])) {
throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
}
$currentStyle = $style;
return \NumberFormatter::create($locale, $styleValues[$style]);
}
}