diff options
Diffstat (limited to 'inc/Twig/Extensions/Extension/Intl.php')
-rw-r--r-- | inc/Twig/Extensions/Extension/Intl.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/inc/Twig/Extensions/Extension/Intl.php b/inc/Twig/Extensions/Extension/Intl.php new file mode 100644 index 00000000..40f7fc20 --- /dev/null +++ b/inc/Twig/Extensions/Extension/Intl.php | |||
@@ -0,0 +1,66 @@ | |||
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 | |||
12 | class Twig_Extensions_Extension_Intl extends Twig_Extension | ||
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 | |||
21 | /** | ||
22 | * Returns a list of filters to add to the existing list. | ||
23 | * | ||
24 | * @return array An array of filters | ||
25 | */ | ||
26 | public function getFilters() | ||
27 | { | ||
28 | return array( | ||
29 | 'localizeddate' => new Twig_Filter_Function('twig_localized_date_filter', array('needs_environment' => true)), | ||
30 | ); | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Returns the name of the extension. | ||
35 | * | ||
36 | * @return string The extension name | ||
37 | */ | ||
38 | public function getName() | ||
39 | { | ||
40 | return 'intl'; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | function twig_localized_date_filter(Twig_Environment $env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null) | ||
45 | { | ||
46 | $date = twig_date_converter($env, $date, $timezone); | ||
47 | |||
48 | $formatValues = array( | ||
49 | 'none' => IntlDateFormatter::NONE, | ||
50 | 'short' => IntlDateFormatter::SHORT, | ||
51 | 'medium' => IntlDateFormatter::MEDIUM, | ||
52 | 'long' => IntlDateFormatter::LONG, | ||
53 | 'full' => IntlDateFormatter::FULL, | ||
54 | ); | ||
55 | |||
56 | $formatter = IntlDateFormatter::create( | ||
57 | $locale !== null ? $locale : Locale::getDefault(), | ||
58 | $formatValues[$dateFormat], | ||
59 | $formatValues[$timeFormat], | ||
60 | $date->getTimezone()->getName(), | ||
61 | IntlDateFormatter::GREGORIAN, | ||
62 | $format | ||
63 | ); | ||
64 | |||
65 | return $formatter->format($date->getTimestamp()); | ||
66 | } | ||