Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to change currency format pattern, currency symbol and currency symbol position programmatically. I found some data in folder vendor\magento\zendframework1\library\Zend\Locale\Data.

For example if I change the format in fr_FR.xml by following code its is reflecting in frontend.

<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>

But I want to know how to override the default fr_FR.xml (vendor\magento\zendframework1\library\Zend\Locale\Data\fr_FR.xml)

Kindly let me know if anyone knows the way to do.

like image 574
Shan13 Avatar asked Sep 08 '16 07:09

Shan13


1 Answers

May not be a complete solution, but this must be a good start. Below is the order of the code flow.

  • public function formatTxt on module-directory/Model/Currency.php . This function calls toCurrency which in turn calls to
  • public function toCurrency on zendframework1/library/Zend/Currency.php

when you find the function, you will see $options array variable which contains all the necessary info's for formatting the price values. Below is the var_dump of $options.

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"
}

So for moving the currency symbols you can override

public function formatPrecision in the DI.xml

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

and pass the options array with necessary values.

e.g: $options['position'] = 16 will move the currency symbol to the right of the currency value (16.24$)

Likewise pass the necessary array options to override.

like image 85
Franc Avatar answered Sep 21 '22 16:09

Franc