From 01869215d33d9675501296d4c30cb28e3b0ae8eb Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 2 Nov 2023 08:04:42 -0700 Subject: [PATCH] feat: Add support for currency internationalization (#39) * feat: support multicurrency separator * feat: support multicurrency separator optimize --------- Co-authored-by: sam --- Api/RequestHandler.php | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Api/RequestHandler.php b/Api/RequestHandler.php index 1d75a13..b54d966 100644 --- a/Api/RequestHandler.php +++ b/Api/RequestHandler.php @@ -44,6 +44,13 @@ class RequestHandler extends \NoFraud\Connect\Api\Request\Handler\AbstractHandle 'vi' => 'Visa', ]; + /** + * Japan locale code + */ + private const JAPAN_LOCALE_CODE = 'ja_JP'; + + protected $_localeResolver; + /** * Constructor * @@ -60,7 +67,8 @@ public function __construct( \Magento\Directory\Model\Currency $currency, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\Customer $customer, - \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface $orderCollectionFactory + \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface $orderCollectionFactory, + \Magento\Framework\Locale\ResolverInterface $localeResolver ) { parent::__construct($logger, $curl); @@ -69,6 +77,7 @@ public function __construct( $this->customerRepository = $customerRepository; $this->customer = $customer; $this->orderCollectionFactory = $orderCollectionFactory; + $this->_localeResolver = $localeResolver; } /** @@ -407,8 +416,32 @@ protected function formatTotal($amount) if (empty($amount)) { return; } + + $value = $this->currency->formatTxt($amount, ['display' => \Magento\Framework\Currency::NO_SYMBOL]); + + $separatorComa = strpos($value, ','); + $separatorDot = strpos($value, '.'); + $price = $value; - return $this->currency->formatTxt($amount, ['display' => \Magento\Framework\Currency::NO_SYMBOL]); + if ($separatorComa !== false && $separatorDot !== false) { + if ($separatorComa > $separatorDot) { + $price = preg_replace("/(\d+)\.(\d+),(\d+)/", "$1,$2.$3", $value); + } + } elseif ($separatorComa !== false) { + $locale = $this->_localeResolver->getLocale(); + + /** + * It's hard code for Japan locale. + */ + $price = number_format( + (float) + str_replace(',', $locale === self::JAPAN_LOCALE_CODE ? '' : '.', $value), + 2, + '.', + ',' + ); + } + return $price; } /**