for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Mero\Bundle\BaseBundle\Twig;
use Symfony\Component\Intl\Intl;
/**
* Country filter for Twig.
*
* @author Rafael Mello <[email protected]>
* @license https://github.com/merorafael/MeroBaseBundle/blob/master/LICENSE MIT license
*/
class CountryExtension extends \Twig_Extension
{
public function __construct()
if (!class_exists('\Locale')) {
throw new \RuntimeException('The country extension is needed to use intl-based filters.');
}
public function getFilters()
return array(
new \Twig_SimpleFilter('country', [
$this,
'getCountryName',
]),
);
* Return the country name using the Locale class.
* @param string $isoCode Country ISO 3166-1 alpha 2 code
* @param null|string $locale Locale code
* @return null|string Country name
public function getCountryName($isoCode, $locale = null)
if ($isoCode === null) {
return;
return ($locale === null)
? Intl::getRegionBundle()->getCountryName($isoCode)
: Intl::getRegionBundle()->getCountryName($isoCode, $locale);
public function getName()
return 'country_extension';