【问题标题】:Sf2 : Intl component, formatCurrencySf2 : Intl 组件,formatCurrency
【发布时间】: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 方法?

【问题讨论】:

标签: php symfony intl


【解决方案1】:

整个Intl Component 只是您没有安装intl extension 的情况的替换层。

所以你的代码应该是这样的:

<?php
namespace SE\AppBundle\Twig;

use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\NumberFormatter\NumberFormatter;

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([
            'code' => $currency_code
        ]);

        $price = $price*$exchange_rate->getRate();

        if(false === extension_loaded('intl')) {
            $formatter = new NumberFormatter('en', NumberFormatter::CURRENCY);
        } else {
            $formatter = new \NumberFormatter(
                $request->getLocale(),
                \NumberFormatter::CURRENCY
            );
        }

        return $formatter->formatCurrency($price, $currency_code);
    }

    public function getName()
    {
        return 'price_extension';
    }
}

【讨论】:

  • 谢谢,我现在明白了:)。
猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 2016-08-20
  • 2021-09-17
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
相关资源
最近更新 更多