【问题标题】:Zend Currency in Euro with English localeZend 欧元货币,英语语言环境
【发布时间】:2011-06-10 14:06:22
【问题描述】:

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

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

这个我也试过了

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

但如果我尝试

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

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

【问题讨论】:

    标签: zend-framework zend-currency


    【解决方案1】:

    这行得通:

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

    【讨论】:

      【解决方案2】:

      我扩展了Zend_Currency 并覆盖了getName()getSymbol() 方法,以便它们(当区域设置更改时Zend_Currency 也是如此,这是我们不想要的......)从@ 返回“新鲜”数据987654325@每次我提出请求。

      在下面的示例类中,您可以创建Default_Model_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();
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        这似乎可行:-

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

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

        【讨论】:

        • 您询问了有关更改货币的问题,特别是关于 getSymbol() 的结果,我回答了这个问题并引导您为自己制定最终答案,所以我对您接受了您的自己的答案超过我的。很高兴你最终到达那里:)
        • 您的回答无效。我的做到了。你没有解释原因
        • 我的回答适用于您在原始问题中提出的问题。没关系,这没什么大不了的。
        猜你喜欢
        • 2012-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多