【问题标题】:ZF2 Currency FormatZF2 货币格式
【发布时间】:2013-02-26 22:39:58
【问题描述】:

我在我的项目中使用 ZF2。这是一个电子商务网站。所以我在处理货币。

在 ZF2 中有一个名为 currencyFormat() 的视图助手

我来自土耳其,所以我的主要货币格式是 TRY(这是土耳其里拉的 ISO 代码)。但在土耳其,我们不使用 TRY 作为货币图标。图标是“$”代表美元,€ 代表“EUR”和“TL”代表土耳其里拉 (TRY)。

因此,当我为 TRY 设置货币格式时,我会在视图脚本中这样做:

<?php
echo $this->currencyFormat(245.40, 'TRY', 'tr_TR');
?>

此代码的结果是“245.40 TRY”。但它必须是“245.40 TL

有没有办法解决这个问题?我不想使用替换功能。

【问题讨论】:

  • 将 TRY 替换为 TL 会发生什么?
  • 它不打印任何想法。由于助手使用 INTL 扩展,它只接受货币的 ISO 代码。
  • TL 将不是官方的 ISO 4217 货币代码指标,因此将不起作用。如果确实如此,我会认为这种行为是 PHP 核心中的一个错误。虽然我不知道 turkey,但如果它真的是 TL 而不是 TRY,那么你应该提交一个错误报告!

标签: php zend-framework2 currency


【解决方案1】:

我猜当您说I do not want to use replacement function 时,您的意思是每次调用助手时都执行str_replace 会很费力。解决方案是用您自己的替换助手。这是一个快速的方法

首先创建一个您自己的助手,它扩展现有助手并在必要时处理替换...

<?php
namespace Application\View\Helper;

use Zend\I18n\View\Helper\CurrencyFormat;

class MyCurrencyFormat extends CurrencyFormat
{
    public function __invoke(
        $number,
        $currencyCode = null,
        $showDecimals = null,
        $locale       = null
    ) {
       // call parent and get the string
       $string = parent::__invoke($number, $currencyCode, $showDecimals, $locale);
       // format to taste and return
       if (FALSE !== strpos($string, 'TRY')) {
           $string = str_replace('TRY', 'TL', $string);
       }
       return $string;
    }
}

然后在 Module.php 中,实现 ViewHelperProviderInterface,并为它提供帮助程序的详细信息

//Application/Module.php
class Module implements \Zend\ModuleManager\Feature\ViewHelperProviderInterface
{

     public function getViewHelperConfig()
     {
         return array(
             'invokables' => array(
                  // you can either alias it by a different name, and call that, eg $this->mycurrencyformat(...)
                  'mycurrencyformat'  => 'Application\View\Helper\MyCurrencyFormat',
                  // or if you want to ALWAYS use your version of the helper, replace the above line with the one below, 
                  //and all existing calls to $this->currencyformat(...) in your views will be using your version
                  // 'currencyformat'  => 'Application\View\Helper\MyCurrencyFormat',
              ),
         );
     }
}

【讨论】:

    【解决方案2】:

    截至 2012 年 3 月 1 日,土耳其里拉的标志是 TRY。 http://en.wikipedia.org/wiki/Turkish_lira

    所以我认为 ZF 的输出是正确的。

    【讨论】:

    • ZF 是对的。但是,TRY(2 个字符是国家,第 3 个字符代表货币名称,即“YENI LIRA”)是国际的,ZF 是完全正确的。但是,我们在本地使用 TL,没有人会理解 TRY,仅此而已。 @Crisp 的解决方案完全解决了它,但它是需要的。
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 2012-01-22
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多