2013-02-26 212 views
0

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

在ZF2有一个名为currencyFormat()

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

所以,当我格式化货币TRY我做它像这样在视图脚本:

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

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

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

+0

当你用TL代替TRY会发生什么? – AmazingDreams 2013-02-26 22:41:58

+0

它不打印任何想法。由于助手使用INTL扩展,它只接受货币的ISO代码。 – Valour 2013-02-26 22:44:46

+0

'TL'不会是官方的ISO 4217货币代码指示器,因此无法使用。如果真的如此,我会认为这是PHP核心中的一个错误。我不知道火鸡,但如果它真的TL而不是TRY,那么你应该提交一份错误报告! – Sam 2013-02-26 22:47:02

回答

1

我猜当你说I do not want to use replacement function你的意思是这是很费力的做str_replace每次调用辅助时间。解决办法是用你自己的替换助手。这里有一个快速如何

首先在Module.php创建自己的助手,其扩展了现有的帮手,如果需要处理更换...

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

然后,实施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', 
      ), 
     ); 
    } 
} 
+0

谢谢@Crisp。 – smozgur 2015-04-12 18:18:48

0

由于1名2012年3月签了新土耳其里拉是TRY。 http://en.wikipedia.org/wiki/Turkish_lira

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

+0

ZF是对的。然而,TRY(2个字符是国家,第3个代表货币名称,即'YENI LIRA')是国际化的,ZF是完全正确的。但是,我们在本地使用TL,并且没有人会理解TRY,这就是它的全部。 @Cryp的解决方案完全解决了它,但它是必要的。 – smozgur 2015-04-12 18:23:29