基于 HTTP 请求 getLocale 抛出 IllegalArgumentException 的货币

Currency based on HTTP request getLocale throwing IllegalArgumentException

我正在尝试根据语言环境、servlet 中的货币代码获取货币符号。

我尝试执行以下操作,但它正在抛出 IllegalArgumentException

Currency.getInstance(request.getlocale())

区域设置的语言为 "en",但国家/地区值为空。

如何获取语言环境、货币代码的货币符号?

为没有国家/地区的地区要求货币是没有意义的。

在您的问题中,例如“英语”的货币是什么?这可能是 USD(美国)、AUD(澳大利亚)、UKP(英国)、EUR(爱尔兰)、CAD(加拿大)等。换句话说,如果不知道国家/地区,您无法选择货币,并且在事实上,货币只取决于国家而不是语言。国家/地区有一种官方货币,但可以有不止一种语言,例如加拿大有 fr_CAen_CA.

此外,它在 Javadoc 中的说明非常清楚(我的重点):

public static Currency getInstance​(Locale locale)

Returns the Currency instance for the country of the given locale. The language and variant components of the locale are ignored. The result may vary over time, as countries change their currencies. For example, for the original member countries of the European Monetary Union, the method returns the old national currencies until December 31, 2001, and the Euro from January 1, 2002, local time of the respective countries.

The method returns null for territories that don't have a currency, such as Antarctica.

Parameters:

    locale - the locale for whose country a Currency instance is needed

Returns:

    the Currency instance for the country of the given locale, or null Throws:

    NullPointerException - if locale is null

    IllegalArgumentException - if the country of the given locale is not a supported ISO 3166 country code.