java util locale,java.util.Locale的第一项为空

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 countries(Locale currentLocale) {

Map countries = new TreeMap();

for (Locale locale : Locale.getAvailableLocales()) {

countries.put(locale.getCountry(),

locale.getDisplayCountry(currentLocale));

}

countries.remove("");

return countries;

}

JVM version - (build 1.7.0_09-b05)

解决方案

The javadocs basically cover this.

For locale.getCountry() you find:

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.

And for locale.getDisplayCountry() you find:

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.

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值