2016-01-22 129 views
2

我有一些国家变量,例如“法国”。 我想获取国家代码,在这个例子中,我想获得“FR”或“fr”。Android Locale国名到国家代码

我想:

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

但它返回:

FRANCE 

什么我失踪?

谢谢!

回答

2

你可以使用内置的Locale类:

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

打印 “瑞士” 的例子。请注意,我没有提供一种语言。

那么你可以反向查找要做的就是从现有的国家建立一个地图:对这个职位POST发现

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")); 

}

答案。希望能帮助到你。