2016-05-16 1 views
-1

私は通貨記号を取得するために、twl拡張子でintlコンポーネントを使用しています。Sf2:intlコンポーネント、formatCurrency

よくわかりやすいようにhereです。

しかし、私がしたいのは、通貨/ローカルに基づいて価格をフォーマットすることです。

国際コンポーネント(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メソッドを使用することができる可能性のメソッドformatCurrencyは確かにありますか?

+0

チェック:

だからあなたのコードは次のようになります。私が言ったように、私はコンポーネントを使用しています。しかし、その後、私は間違いを犯した通貨バンドルを手に入れています。私が知る必要があるのは、どのようにコンポーネントからのものを使うことができるかということだけです。 https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php – Brieuc

答えて

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'; 
    } 
} 
+0

ありがとう、私は今それを得る:)。 – Brieuc

関連する問題