Skip to content

Added a fallback Twig extension when PHP's intl is not available #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: services.php }

# These are the configuration parameters that define the application's behavior
# and which are independent from the underlying technical infrastructure
Expand Down
17 changes: 17 additions & 0 deletions app/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file loads services into the Symfony Container dynamically. This allows
* to check if the PHP Intl extension is loaded in the system. If it's loaded,
* we use the default Twig Intl extension. If it's not loaded, we use a fallback
* Twig extension which doesn't require the Intl extension.
*/

use Symfony\Component\DependencyInjection\Definition;

$serviceClass = extension_loaded('intl') ? 'Twig_Extensions_Extension_Intl' : 'AppBundle\\Twig\\IntlExtension';

$definition = new Definition($serviceClass);
$definition->setPublic(false);
$definition->addTag('twig.extension');
$container->setDefinition('app.twig.intl_extension', $definition);
6 changes: 0 additions & 6 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ services:
tags:
- { name: twig.extension }

app.twig.intl_extension:
public: false
class: Twig_Extensions_Extension_Intl
tags:
- { name: twig.extension }

app.redirect_to_preferred_locale_listener:
class: AppBundle\EventListener\RedirectToPreferredLocaleListener
arguments: ['@router', '%app_locales%', '%locale%']
Expand Down
74 changes: 74 additions & 0 deletions src/AppBundle/Twig/IntlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AppBundle\Twig;

/**
* This Twig extension adds a new 'localizeddate' filter to format date/time
* objects. These formats are for English language only because this extension
* is exclusively used when the system doesn't have the PHP Intl extension loaded.
*
* @author Javier Eguiluz <[email protected]>
*/
class IntlExtension extends \Twig_Extension
{
private $dateFormats = array(
'full' => 'l, F d, Y',
'long' => 'F d, Y',
'medium' => 'M d, Y',
'short' => 'M/d/yy',
'none' => '',
);

private $timeFormats = array(
'full' => 'r',
'long' => 'h:i:s a e',
'medium' => 'h:i:s a',
'short' => 'h:i a',
'none' => '',
);

/**
* {@inheritdoc}
*/
public function getFilters()
{
return array(
new \Twig_SimpleFilter('localizeddate', array($this, 'getLocalizedDate')),
);
}

/**
* Formats the given date string/object according to some predefined
* date/time formats.
*
* @param mixed $date
* @param string $dateFormat
* @param string $timeFormat
*
* @return string
*/
public function getLocalizedDate($date, $dateFormat = 'medium', $timeFormat = 'medium')
{
$date = ($date instanceof \DateTimeInterface) ? $date : new \DateTime($date);

return $date->format($this->dateFormats[$dateFormat].' '.$this->timeFormats[$timeFormat]);
}

/**
* {@inheritdoc}
*/
public function getName()
{
// the name of the Twig extension must be unique in the application
return 'app.intl_extension';
}
}