I'm using in a twig extension the intl component to get the symbol of the currency.
Pretty simple as it is well explained here.
But what i would like to do is to format the price based on the currency / local.
There is indeed a method formatCurrency in the intl component (NumberFormatter class)
<?php
namespace SE\AppBundle\Twig;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;
class PriceExtension extends \Twig_Extension
{
private $em;
private $requestStack;
public function __construct(EntityManager $em, RequestStack $requestStack)
{
$this->em = $em;
$this->requestStack = $requestStack;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
);
}
public function priceFilter($price)
{
$request = $this->requestStack->getCurrentRequest();
$currency_code = $request->cookies->get('currency');
$exchange_rate = $this->em->getRepository('ApiBundle:ExchangeRates')->findOneBy(array('code' => $currency_code));
$price = $price*$exchange_rate->getRate();
// Get the currency symbol
// $symbol = Intl::getCurrencyBundle()->getCurrencySymbol($currency_code);
// $price = $symbol.$price;
// Undefined formatCurrency method
$price = Intl::getCurrencyBundle()->formatCurrency($price, $currency_code);
return $price;
}
public function getName()
{
return 'price_extension';
}
}
How could i be able to use the formatCurrency method?