java.util.Locale 有一个空的第一项
我正在尝试从 JVM 获取所有语言环境以填充 Country 下拉列表.第一项是空的非空对象.它不为空,因为我正在使用 TreeMap 集合来添加国家/地区缩写和(可显示)名称.看看下面的集合是什么.
I am trying to get all the locales from the JVM to populate a Country drop down. The first item is an empty not null object. It isn't null because I am using a TreeMap Collection to add the country Abbreviations and (Displayable) Names. Look below to see what the collection is.
{=, AE=United Arab Emirates, AL=Albania, AR=Argentina, AT=Austria, AU=Australia, BA=Bosnia and Herzegovina, BE=Belgium, BG=Bulgaria, BH=Bahrain, BO=Bolivia, BR=Brazil,....
这是代码.我删除了那个空对象以确保第一个值不为空.
Here's the code. I remove that empty object to make sure the first value is not empty.
public Map<String, String> countries(Locale currentLocale) {
Map<String, String> countries = new TreeMap<String, String>();
for (Locale locale : Locale.getAvailableLocales()) {
countries.put(locale.getCountry(),
locale.getDisplayCountry(currentLocale));
}
countries.remove("");
return countries;
}
JVM 版本 - (build 1.7.0_09-b05)
JVM version - (build 1.7.0_09-b05)
推荐答案
javadocs 基本上涵盖了这个.
对于 locale.getCountry()
你会发现:
返回此语言环境的国家/地区代码,它应该是空字符串、大写的 ISO 3166 2 位代码或 UN M.49 3 位代码.p>
Returns the country/region code for this locale, which should either be the empty string, an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code.
对于 locale.getDisplayCountry()
你会发现:
返回适合向用户显示的区域设置国家/地区的名称.如果可能,返回的名称将针对默认语言环境进行本地化.例如,如果语言环境是 fr_FR,默认语言环境是 en_US,getDisplayCountry() 将返回France";如果语言环境是 en_US 并且默认语言环境是 fr_FR,getDisplayCountry() 将返回Etats-Unis".如果返回的名称无法针对默认语言环境进行本地化(例如,我们没有克罗地亚的日文名称),则此函数将使用英文名称,并使用 ISO 代码作为最后的值.如果语言环境未指定国家/地区,则此函数返回空字符串.
Returns a name for the locale's country that is appropriate for display to the user. If possible, the name returned will be localized for the default locale. For example, if the locale is fr_FR and the default locale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis". If the name returned cannot be localized for the default locale, (say, we don't have a Japanese name for Croatia), this function falls back on the English name, and uses the ISO code as a last-resort value. If the locale doesn't specify a country, this function returns the empty string.
您无法看到"的那个没有 ISO 代码,并且由于它无法本地化到您的语言环境并且也没有英文名称(或者,没有指定国家).用空字符串结束两者是完全有效的(虽然,相当没用).
The one you can't "see" doesn't have an ISO code and is falling back to that because it can't be localized to your locale and doesn't have an english name either (or, doesn't have a country specified). It is perfectly valid for you to wind up with empty strings for both (though, fairly useless).
相关文章