Zend货币为欧元,语言为英语



我很可能完全不理解Zend在处理货币方面的做法。

我想做的是有一个Zend_Currency对象,其中活动货币是EURO,语言是英语(应用程序的区域设置是en_GB)。Zend_Currency连接到一个区域设置,如果创建一个具有英语区域设置的Zend_Curtain,我就不能拥有EUR.

我也试过这个

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

但如果我尝试

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

而且没有改变货币的方法。

这很有效:

$this->currency = new Zend_Currency('en_GB');
$this->currency->setFormat(array('currency' =>  'EUR', 'name' =>'EUR', 'symbol' => '€'));

我扩展了Zend_Currency,并覆盖了getName()getSymbol()方法,这样它们(就像Zend_Currency在区域设置更改时一样,我们不希望..)每次请求时都会从Zend_Locale_Data返回"新鲜"数据。

在下面的示例类中,您可以创建Default_Model_Currency()的实例,并将"currency"选项设置为任何有效的货币(如欧元、美元等),同时保持相同的区域设置。

<?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();
    }
}

这似乎有效:-

$currency = new Zend_Currency();
$currency->setFormat(array('currency' => 'EUR', 'symbol' => '€'));

您可以向getFormat()传递一个数组来设置这样的选项。http://framework.zend.com/manual/en/zend.currency.options.html

相关内容

  • 没有找到相关文章

最新更新