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

标签 php magento zend-framework magento2

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

例如,如果我通过以下代码更改 fr_FR.xml 中的格式,它会反射(reflect)在前端。

<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 toCurrency on zendframework1/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"
}

所以为了移动您可以覆盖的货币符号

DI.xml 中的公共(public)函数 formatPrecision

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

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

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

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

关于php - Magento 2 如何根据语言环境自定义货币符号和格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39384750/

相关文章:

php - 将外部脚本与 Zend Framework 集成

PHP、MySQL - DATE 字段中的多条记录

php - Magento Paypal 订单已下达但重定向不正确

magento - 为 Magento 的 NGINX 服务器上的 CSS 和 JS 文件启用 GZIP

php - Magento 错误 : SQLSTATE[HY000]: General error: 1

zend-framework - Zend_File_Transfer是否可以处理同一类的两个不同的过滤器?

amazon-s3 - 如何通过 Zend_Service_Amazon_S3 访问 Amazon s3 私有(private)存储桶对象

php - 从 json 中检索键和值

javascript - .done ajax 给我一个错误

php - 为远程网页保存 cookie