Jas*_*ins 74 java country-codes internationalization iso-3166
有没有人知道一个免费提供的java 1.5软件包,它提供了一个ISO 3166-1国家代码列表作为枚举或EnumMap?具体来说,我需要"ISO 3166-1-alpha-2代码元素",即2个字符的国家代码,如"us","uk","de"等.创建一个很简单(虽然单调乏味),但如果在阿帕奇的土地上已经有一个标准的,它可以节省一点时间.
Tak*_*aki 101
现在,作为Java枚举的国家代码(ISO 3166-1 alpha-2/alpha-3/numeric)列表的实现可以在Apache许可证版本2.0的GitHub上获得.
例:
CountryCode cc = CountryCode.getByCode("JP");
System.out.println("Country name = " + cc.getName()); // "Japan"
System.out.println("ISO 3166-1 alpha-2 code = " + cc.getAlpha2()); // "JP"
System.out.println("ISO 3166-1 alpha-3 code = " + cc.getAlpha3()); // "JPN"
System.out.println("ISO 3166-1 numeric code = " + cc.getNumeric()); // 392
Run Code Online (Sandbox Code Playgroud)
最后编辑 2016年6月9日
CountryCode枚举与其他Java枚举,LanguageCode(ISO 639-1),LanguageAlpha3Code(ISO 639-2),LocaleCode,ScriptCode(ISO 15924)和CurrencyCode(ISO 4217)打包到com.neovisionaries.i18n 并注册到Maven Central库.
Maven的
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-i18n</artifactId>
<version>1.22</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
摇篮
dependencies {
compile 'com.neovisionaries:nv-i18n:1.22'
}
Run Code Online (Sandbox Code Playgroud)
GitHub上
https://github.com/TakahikoKawasaki/nv-i18n
的Javadoc
http://takahikokawasaki.github.com/nv-i18n/
OSGi的
Bundle-SymbolicName: com.neovisionaries.i18n
Export-Package: com.neovisionaries.i18n;version="1.22.0"
Run Code Online (Sandbox Code Playgroud)
McD*_*ell 46
此代码在Sun Java 6中获得242个国家:
String[] countryCodes = Locale.getISOCountries();
Run Code Online (Sandbox Code Playgroud)
尽管ISO网站声称有249个ISO 3166-1-alpha-2代码元素,但javadoc链接到相同的信息.
以下是我使用国家/地区代码+国家/地区名称生成枚举的方法:
package countryenum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class CountryEnumGenerator {
public static void main(String[] args) {
String[] countryCodes = Locale.getISOCountries();
List<Country> list = new ArrayList<Country>(countryCodes.length);
for (String cc : countryCodes) {
list.add(new Country(cc.toUpperCase(), new Locale("", cc).getDisplayCountry()));
}
Collections.sort(list);
for (Country c : list) {
System.out.println("/**" + c.getName() + "*/");
System.out.println(c.getCode() + "(\"" + c.getName() + "\"),");
}
}
}
class Country implements Comparable<Country> {
private String code;
private String name;
public Country(String code, String name) {
super();
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Country o) {
return this.name.compareTo(o.name);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您已经依赖Java语言环境,那么我建议使用简单的HashMap而不是为国家/地区等创建新类.
如果我只依赖Java Localization,那么我将如何使用它:
private HashMap<String, String> countries = new HashMap<String, String>();
String[] countryCodes = Locale.getISOCountries();
for (String cc : countryCodes) {
// country name , country code map
countries.put(new Locale("", cc).getDisplayCountry(), cc.toUpperCase());
}
Run Code Online (Sandbox Code Playgroud)
填写地图后,您可以在需要时从国家/地区名称获取ISO代码.或者您也可以将其作为国家/地区名称映射的ISO代码,只需相应地修改"put"方法即可.