Android Locale国家/地区代码的国家/地区名称

时间:2016-01-22 11:42:46

标签: android localization

我有一些国家变数,"法国"例如。 我想得到国家代码,在这个例子中,我想得到" FR"或" fr"。

我试过了:

 Locale locale = new Locale("", "FRANCE");
 Log.e("test", locale.getCountry());

但它返回:

 FRANCE

我失踪了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用内置的Locale类:

Locale l = new Locale("", "CH");
System.out.println(l.getDisplayCountry());
例如,

打印“瑞士”。请注意,我没有提供语言。

因此,您可以为反向查找做的是从可用的国家/地区构建地图:

public static void main(String[] args) throws InterruptedException {
Map<String, String> countries = new HashMap<>();
for (String iso : Locale.getISOCountries()) {
    Locale l = new Locale("", iso);
    countries.put(l.getDisplayCountry(), iso);
}


System.out.println(countries.get("Switzerland"));
System.out.println(countries.get("Andorra"));
System.out.println(countries.get("Japan"));

}

在此帖POST上找到答案。希望它有所帮助。

相关问题