简体   繁体   English

以与语言环境无关的方式格式化货币符号的推荐方法是什么?

[英]What's the recommended way to format currency symbols in a locale-agnostic way?

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 ):目前,输出格式是区域感知的(与NumberFormat一样):

Monetary amount金额 Format with locale es-ES使用语言环境 es-ES 格式化 Format with locale en-US使用区域设置 en-US 的格式
Money.of(1234, "EUR") Money.of(1234, "EUR") 1.234,00 € 1.234,00 欧元 EUR1,234.00 1,234.00 欧元

Generated with this gist这个要点生成

Questions:问题:

  1. Do you see any problem with this?你觉得这有什么问题吗? Perhaps I'm not considering some consequences of this decision也许我没有考虑这个决定的一些后果
  2. What's the best way of accomplishing this with Moneta?使用 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 :我已经能够通过覆盖AmountFormatQueryBuilder中的语言环境来强制它使用我喜欢的语言环境来获得所需的输出:

    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 .但这需要有一个Map<Currency, Locale>来保存应该从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好的,我认为这个问题的答案在这里: https ://github.com/JavaMoney/jsr354-ri/blob/master/moneta-core/src/main/asciidoc/userguide.adoc#42-registering-your- 自有格式

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通过使用 JSR 的 SPI 机制注册新格式,我们可以覆盖默认格式并让它按照我们想要的方式工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM