Java 如果你有 ISO 国家代码`US`、`FR`,你如何获得区域设置代码(`Locale.US`、`Locale.FRANCE`)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6556792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If you have the ISO country code `US`, `FR`, how do you get the Locale code (`Locale.US`, `Locale.FRANCE`)?
提问by Pascal
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:
如果有国家代码US
,FR
(ISO-3166-1 alpha-2国家代码),你怎么弄的地区代码(Locale.US
,Locale.FRANCE
)做这样的事情:
System.out.println(DecimalFormat.getCurrencyInstance(Locale.US).format(12.34));
System.out.println(DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(12.34));
.34
12,34
采纳答案by JB Nizet
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.
您不能,因为 Locale 用于保存语言,而不是国家/地区。它可以保存特定国家/地区的语言,以及该国家/地区的特定变体,但首先是语言。而且语言和国家之间没有一对一的关系。大多数语言在不同的国家使用,许多国家有多种语言。
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.
如果您有某种语言的国家/地区代码,则可以使用new Locale(code)
. 但是对于国家代码,您所能做的就是调用getAvailableLocales
,遍历结果,然后找到一个包含您的国家代码的结果。但可能有几个。
回答by aldrin
You can either create the locale,
您可以创建语言环境,
new Locale("en", "US")
new Locale("fr", "FR")
or
或者
iterate through Locale.getAvailableLocales()
till you find your locale and then use that instance.
迭代Locale.getAvailableLocales()
直到找到您的语言环境,然后使用该实例。
回答by Bozho
In Java7 there is the Locale.Builder
, but before that there isn't an easy way. You can, however create a utility method:
在 Java7 中有Locale.Builder
,但在此之前没有简单的方法。但是,您可以创建一个实用程序方法:
- loop
Locale.getAvailableLocales()
- for each check if
locale.getCountryCode().equals(countryCodeParam)
and return it
- 环形
Locale.getAvailableLocales()
- 每次检查是否
locale.getCountryCode().equals(countryCodeParam)
并返回
回答by Michael Borgwardt
A locale is specified most importantly by the ISO-639 language code, possible also a ISO-3166 country code and a variant. The Locale
class has constructors that take either only a language code, or additionally a country code, or additionally a variant.
区域设置最重要的是由 ISO-639 语言代码指定,也可能是 ISO-3166 国家/地区代码和变体。本Locale
类有采取任何只有一个语言代码,或者另外一个国家代码,或者另外一个变体的构造。
If you only have the country code, you first need a map that converts it to a language code - but that does not necessarily produce a unique result, many countries use more than one official language.
如果您只有国家/地区代码,则首先需要将其转换为语言代码的地图 - 但这不一定会产生唯一的结果,许多国家/地区使用不止一种官方语言。