Archive for June, 2008

Patch up a Currency Format of Magento and Zend Framework for JPY

Saturday, June 28th, 2008

DesignWalker の管理人さんから依頼がありまして、Magento というECサイト構築エンジンで「ロケールを日本にしても価格表示に小数点以下が表示される」という問題にパッチを当てました。

Magento って全く知りませんで、中身を見るとどうやら Zend フレームワークを使っているご様子。こっちも初めてでいろいろ複雑です。

結局 Magento の問題というよりは Zend 側の Currency クラスに問題がありました。

1.ロケール情報XMLファイルの currencyFormat に小数点が含まれていました。
2.ところが、実際の表示には currencyFormat が使われていません。

つーことで、1.を修正しつつ、2.に対応するためのパッチを当てるはめに。

とりあえずの応急処置ですが晒しときます。
本格的にやるにはちょっと時間がかかりそうなのでこの辺でw

/lib/Zend/Locale/Data/ja.xml

<currencyFormats>
  <currencyFormatLength>
       <currencyFormat>
      <pattern>¤#,##0.00</pattern>
    </currencyFormat>
  </currencyFormatLength>
</currencyFormats>

/lib/Zend/Currency.php

protected function _updateFormat()
{
    $locale = empty($this->_options['format']) ? $this->_locale : $this->_options['format'];

    //getting the format information of the currency
    $format = Zend_Locale_Data::getContent($locale, ‘currencynumber’);

    iconv_set_encoding(’internal_encoding’, ‘UTF-8′);
    if (iconv_strpos($format, ‘;’)) {
        $format = iconv_substr($format, 0, iconv_strpos($format, ‘;’));
    }

    // Patched a currency format for JPY by YungSang
    if (!strpos($format, ‘.’)) {
        $this->_options['precision'] = 0;
    }

    //knowing the sign positioning information
    if (iconv_strpos($format, ‘¤’) == 0) {
        $position = self::LEFT;
    } else if (iconv_strpos($format, ‘¤’) == iconv_strlen($format)-1) {
        $position = self::RIGHT;
    }

    return $position;
}