Magento 2如何根据区域设置自定义货币符号和格式

时间:2016-09-08 07:17:33

标签: php magento zend-framework magento2

我想以编程方式更改货币格式模式,货币符号和货币符号位置。我在文件夹vendor \ magento \ zendframework1 \ library \ Zend \ Locale \ Data中找到了一些数据。

例如,如果我通过以下代码更改fr_FR.xml中的格式,则会反映在前端。

<numbers>
        <currencyFormats numberSystem="latn">
            <currencyFormatLength>
                <currencyFormat type="standard">
                    <pattern>¤ #,##0.00</pattern>
                </currencyFormat>
                <currencyFormat type="accounting">
                    <pattern>¤ #,##0.00;(¤ #,##0.00)</pattern>
                </currencyFormat>
            </currencyFormatLength>
            <unitPattern count="one">{0} {1}</unitPattern>
            <unitPattern count="other">{0} {1}</unitPattern>
        </currencyFormats>
        <currencies>
            <currency type="GBP">
                <displayName>livre sterling</displayName>
                <displayName count="one">livre sterling</displayName>
                <displayName count="other">livres sterling</displayName>
                <symbol>£</symbol>
            </currency>
        </currencies>
</numbers>

但我想知道如何覆盖默认的fr_FR.xml(vendor \ magento \ zendframework1 \ library \ Zend \ Locale \ Data \ fr_FR.xml)

如果有人知道该怎样做,请告诉我。

1 个答案:

答案 0 :(得分:4)

可能不是一个完整的解决方案,但这必须是一个良好的开端。 以下是代码流的顺序。

    模块目录/ Model / Currency.php上的
  • public function formatTxt。此函数调用toCurrency,而后者又调用
  • zendframework1 / library / Zend / Currency.php 上的
  • public function toCurrency

当您找到该函数时,您将看到$ options数组变量,其中包含用于格式化价格值的所有必要信息。以下是$ options的var_dump。    array(12) { ["position"]=> int(16) ["script"]=> NULL ["format"]=> NULL ["display"]=> int(2) ["precision"]=> int(2) ["name"]=> string(9) "US Dollar" ["currency"]=> string(3) "USD" ["symbol"]=> string(1) "$" ["locale"]=> string(5) "en_GB" ["value"]=> int(0) ["service"]=> NULL ["tag"]=> string(11) "Zend_Locale" }

因此,要移动货币符号,您可以覆盖

带有DI.xml的

public function formatPrecision  <preference for="Magento\Directory\Model\Currency" type="Yourpack\Custom\Model\Currency" />

并使用必要的值传递options数组。

例如:$options['position'] = 16 will move the currency symbol to the right of the currency value (16.24$) 同样传递必要的数组选项以覆盖。

相关问题