【发布时间】:2016-09-12 01:47:55
【问题描述】:
我在树枝扩展中使用 intl 组件来获取货币符号。
很简单,因为它解释得很好here。
但我想做的是根据货币/本地格式化价格。
intl组件中确实有方法formatCurrency(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';
}
}
我怎样才能使用 formatCurrency 方法?
【问题讨论】:
-
再次检查。正如我提到的,我正在使用该组件。但是,如果我得到的是我做错的地方的货币包。我只需要知道如何使用组件中的那个。 github.com/symfony/symfony/blob/master/src/Symfony/Component/…