Magento 2 如何根据语言环境自定义货币符号和格式

Magento 2 How to customize Currency symbol and format based on locale

我想以编程方式更改货币格式模式、货币符号和货币符号位置。我在文件夹 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)

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

可能不是一个完整的解决方案,但这一定是一个好的开始。 下面是代码流的顺序。

  • public function formatTxtmodule-directory/Model/Currency.php 上。此函数调用 toCurrency,后者又调用
  • public function toCurrencyzendframework1/library/Zend/Currency.php

当您找到该函数时,您将看到 $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"
}

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

public function formatPrecision 在 DI.xml

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

并传递具有必要值的选项数组。

例如:$options['position'] = 16 会将货币符号移动到货币值 (16.24$) 的右侧

同样传递必要的数组选项来覆盖。