2013-02-26 327 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