Context
I want to have a standard way of formatting prices regardless of the locale. Currently, the output format is locale-aware (as with NumberFormat
):
Monetary amount | Format with locale es-ES | Format with locale en-US |
---|---|---|
Money.of(1234, "EUR") | 1.234,00 € | EUR1,234.00 |
Generated with this gist
Questions:
- Do you see any problem with this? Perhaps I'm not considering some consequences of this decision
- What's the best way of accomplishing this with Moneta?
Workaround
I've been able to get the desired output by overriding the Locale in the AmountFormatQueryBuilder
to force it use the locale I prefer:
static String format(Money money, Locale locale) {
MonetaryAmountFormat formatter = MonetaryFormats.getAmountFormat(
AmountFormatQueryBuilder.of(locale)
.set(CurrencyStyle.SYMBOL)
.setLocale(EUR_OVERRIDDEN_LOCALE)
.build()
);
return formatter.format(money);
}
but this would require having a Map<Currency, Locale>
that will hold the desired locale for each currency that should be extracted from the MonetaryAmount
.
This solution looks like a workaround and I just want to get some insights from the community.
Ok, I think the answer to this question is here: https://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide.adoc#42-registering-your-own-formats
By registering a new format using the JSR’s SPI mechanism, we can override the default format and have it work the way we want