如果您有 ISO 国家代码 `US`、`FR`,如何获得区域设置代码(`Locale.US`、`Locale.FRANCE`)?
如果你有国家代码US
、FR
(ISO-3166-1 alpha-2国家代码),你如何获得Locale代码(Locale.US
, Locale.FRANCE
) 来做这样的事情:
If you have the country code US
, FR
(ISO-3166-1 alpha-2 country code), how do you get the Locale code (Locale.US
, Locale.FRANCE
) to do something like this:
System.out.println(DecimalFormat.getCurrencyInstance(Locale.US).format(12.34));
System.out.println(DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(12.34));
$12.34
12,34 €
推荐答案
你不能,因为 Locale 用于保存一种语言,而不是一个国家.它可以包含特定国家的语言,以及该国家的特定变体,但它首先是一种语言.语言和国家之间没有一对一的关系.大多数语言在不同的国家/地区使用,许多国家/地区有多种语言.
You can't, because a Locale is used to hold a language, not a country. It can hold a language for a specific country, and for a specific variant in this country, but it's a language first. And there is no one-to-one relationship between a language and a country. Most languages are spoken in various countries, and many countries have several languages.
如果您有一种语言的国家/地区代码,则可以使用 new Locale(code)
.但是有了国家代码,你所能做的就是调用getAvailableLocales
,循环遍历结果,找到一个有你国家代码的.但可能有几个.
If you had the country code for a language, you could use new Locale(code)
. But with a country code, all you can do is call getAvailableLocales
, loop through the results, and find one which has your country code. But there might be several ones.
相关文章