0

通貨を扱う際のZendのアプローチを完全に理解していない可能性が非常に高いです。

私がやりたいのは、アクティブな通貨がEUROで、言語が英語(アプリケーションのロケールはen_GB)であるZend_Currencyオブジェクトを作成することです。Zend_Currencyはロケールに接続されており、英語のロケールでZend_Currencyを作成する場合、EURを使用することはできません。

私もこれを試しました

$this->currency = new Zend_Currency(array('currency' => 'EUR'), "en_GB");

しかし、私がしようとすると

echo $this->currency->getSymbol(); // I get £

また、通貨を変更する方法はありません。

4

3 に答える 3

3

これはうまくいきました:

$this->currency = new Zend_Currency('en_GB');
$this->currency->setFormat(array('currency' =>  'EUR', 'name' =>'EUR', 'symbol' => '€'));
于 2011-06-14T09:21:53.953 に答える
2

メソッドとメソッドを拡張Zend_Currencyして上書きし、リクエストを行うたびに「新しい」データを返すようにしました。getName()getSymbol()Zend_CurrencyZend_Locale_Data

以下のサンプル クラスでは、同じロケールを維持しながら、のインスタンスを作成しDefault_Model_Currency()、'currency' オプションを任意の有効な通貨 (EUR、USD など) に設定できます。

<?php

/*
* Currency class extends Zend_Currency with exchange
* functionality using the Exchange_Service
*
* @author HF Bonnet
*/
class Default_Model_Currency extends Zend_Currency
{
    /*
    * Precision to round currency values with, the
    * default precision is 0
    */
    private $_precision = 0;




    /*
    * Set the currency's precision
    *
    * @attribute int $precision
    * @return Default_Model_Currency
    */
    public function setPrecision($precision)
    {
        $validator = new Zend_Validator_Digits();
        if ($validator->isValid($precision))
            $this->_precision = $precision;

        return $this;
    }



    /*
    * Becouse of problems with zend_currency I've choosen
    * to override __toString
    */
    public function __toString()
    {
        return sprintf(
            '%s %s',
            $this->getShortName(),
            $this->getValue()
        );
    }



    /*
    * Get the full name from a currency, this method is overwitten
    * to support the changing of currency without changing the locale
    *
    * @param string $currency (Optional) Currency name
    * @param string|Zend_Locale $locale
    * @return string $name
    */
    public function getName($currency = null, $locale = null)
    {
        return Zend_Locale_Data::getContent(
            null, 
            'nametocurrency', 
            $this->getShortName()
        );
    }



    /*
    * Get the full name from a currency, this method is overwitten
    * to support the changing of a locale
    *       
    * @param string $currency (Optional) Currency name
    * @param string|Zend_Locale $locale
    * @return string $name
    */
    public function getSymbol($currency = null, $locale = null)
    {
        return Zend_Locale_Data::getContent(
            null, 
            'currencysymbol', 
            $this->getShortName()
        );
    }



    /*
    * Get the localized value from the currency
    *
    * @return string $value
    */
    public function getLocalizedValue()
    {
        return Zend_Locale_Format::toNumber(
            parent::getValue(),
            array(
                'precision' => $this->_precision,
                'locale' => $this->getLocale()
            )
        );
    }



    /*
    * Get the default valuta. First checks in the Zend_Registry
    * for the default valuta, if not found it is fetched from the
    * database.
    * 
    * @return string $defaultCurrency
    */
    public function getDefaultValuta()
    {
        $currency = Zend_Registry::get('currency');

        if (!isset($currency['default'])):
            $className = Zend_Registry::get('prefix')->services . 'Registry';
            $currency['default'] = $className::getInstance()
                                             ->getDb('currencyValuta')
                                             ->getDefaultValuta()
                                             ->getIso();
            Zend_Registry::set(
                'currency', $currency
            );
        endif;

        return $currency['default'];
    }



    /*
    * Exchanges the currency using the rates found in the
    * exchange service.
    *
    * @attribute string $to
    * @return Default_Model_Currency
    */
    public function exchange($to)
    {
        if ($to === $this->getShortName())
            return $this;

        $currencyTo = new Default_Model_Currency(
            array(
                'value' => 0,
                'currency' => $to,
                'precision' => $this->_precision
            )
        );

        // Set the exchange service
        $currencyTo->setService(
            $this->getExchangeService()
        );

        return $currencyTo->add($this);
    }



    /*
    * Get an Default_Model_Settings instance
    * 
    * @return Default_Model_Settings 
    */
    public function getExchangeService()
    {
        $className = Zend_Registry::get('prefix')->services . 'Exchange';
        return $className::getInstance();
    }

}
于 2012-05-17T16:58:25.783 に答える