fixing formatting issues for money without currency (#1932)
This commit is contained in:
@@ -112,6 +112,10 @@ final class LocaleHelper
|
|||||||
$withCurrency = false;
|
$withCurrency = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (false === $withCurrency) {
|
||||||
|
return $this->getMoneyFormatter($withCurrency)->format($amount, NumberFormatter::TYPE_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->getMoneyFormatter($withCurrency)->formatCurrency($amount, $currency);
|
return $this->getMoneyFormatter($withCurrency)->formatCurrency($amount, $currency);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,29 +130,18 @@ final class LocaleHelper
|
|||||||
|
|
||||||
private function getMoneyFormatter(bool $withCurrency = true): NumberFormatter
|
private function getMoneyFormatter(bool $withCurrency = true): NumberFormatter
|
||||||
{
|
{
|
||||||
if (null === $this->moneyFormatter) {
|
|
||||||
$this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($withCurrency) {
|
if ($withCurrency) {
|
||||||
|
if (null === $this->moneyFormatter) {
|
||||||
|
$this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->moneyFormatter;
|
return $this->moneyFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $this->moneyFormatterNoCurrency) {
|
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 = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
|
||||||
|
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_PREFIX, '');
|
||||||
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::CURRENCY_CODE, '');
|
$this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_SUFFIX, '');
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->moneyFormatterNoCurrency;
|
return $this->moneyFormatterNoCurrency;
|
||||||
|
|||||||
Reference in New Issue
Block a user