Skip to content

Commit

Permalink
feat: Add support for currency internationalization (#39)
Browse files Browse the repository at this point in the history
* feat: support multicurrency separator

* feat: support multicurrency separator optimize

---------

Co-authored-by: sam <sam@kammalou.com>
  • Loading branch information
samumaretiya and sam authored Nov 2, 2023
1 parent fb4568b commit 0186921
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions Api/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
Expand All @@ -69,6 +77,7 @@ public function __construct(
$this->customerRepository = $customerRepository;
$this->customer = $customer;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_localeResolver = $localeResolver;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 0186921

Please sign in to comment.