java.util.Locale的第一项为空 [英] java.util.Locale has an empty first item

查看:81
本文介绍了java.util.Locale的第一项为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JVM获取所有语言环境,以填充国家/地区"下拉列表.第一项是一个空的非空对象.它不是null,因为我正在使用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版本-(内部版本1.7.0_09-b05)

JVM version - (build 1.7.0_09-b05)

推荐答案

javadocs 基本上涵盖了这一点.

The javadocs basically cover this.

对于locale.getCountry(),您发现:

返回此语言环境的国家/地区代码,该代码应为空字符串,大写的ISO 3166 2字母代码或UN M.49 3位数代码.

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

这篇关于java.util.Locale的第一项为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆