fixing formatting issues for money without currency (#1932)

This commit is contained in:
Kevin Papst
2020-08-29 16:30:35 +02:00
committed by GitHub
parent 14255d71f7
commit e7da2b408a

View File

@@ -112,6 +112,10 @@ final class LocaleHelper
$withCurrency = false;
}
if (false === $withCurrency) {
return $this->getMoneyFormatter($withCurrency)->format($amount, NumberFormatter::TYPE_DEFAULT);
}
return $this->getMoneyFormatter($withCurrency)->formatCurrency($amount, $currency);
}
@@ -126,29 +130,18 @@ final class LocaleHelper
private function getMoneyFormatter(bool $withCurrency = true): NumberFormatter
{
if (null === $this->moneyFormatter) {
$this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
}
if ($withCurrency) {
if (null === $this->moneyFormatter) {
$this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
}
return $this->moneyFormatter;
}
if (null === $this->moneyFormatterNoCurrency) {
// if anyone knows a better way of achieving this, please let me know!
$this->moneyFormatterNoCurrency = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::CURRENCY_CODE, '');
$this->moneyFormatterNoCurrency->setSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL, '');
$this->moneyFormatterNoCurrency->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '');
// don't understand why this is needed, I'd say this shouldn't be necessary after the above calls
// even worse: the logic changes either between PHP/ICU versions
$pattern = $this->moneyFormatterNoCurrency->getPattern();
$pattern = str_replace(['¤ ', ' ¤', '-¤', ' XXX', 'XXX '], '¤', $pattern);
$pattern = str_replace('XXX', '¤', $pattern);
$pattern = str_replace('¤', '', $pattern);
$this->moneyFormatterNoCurrency->setPattern($pattern);
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_PREFIX, '');
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_SUFFIX, '');
}
return $this->moneyFormatterNoCurrency;