There is an easy way to generate this enum with the language name.
Execute this code to generate the list of enum fields to paste :
/**
* This is the code used to generate the enum content
*/
public static void main(String[] args) {
String[] codes = java.util.Locale.getISOLanguages();
for (String isoCode: codes) {
Locale locale = new Locale(isoCode);
System.out.println(isoCode.toUpperCase() + "(\"" + locale.getDisplayLanguage(locale) + "\"),");
}
}
This still does not answer the question. I was also looking for a kind of enumerator for this, and did not find anything. Some examples using hashtable here, but represent the same as the built-in get
I would go for a different approach. So I created a script in python to automatically generate the list in Java:
#!/usr/bin/python
f = open("data.txt", 'r')
data = []
cc = {}
for l in f:
t = l.split('\t')
cc = { 'code': str(t[0]).strip(),
'name': str(t[1]).strip()
}
data.append(cc)
f.close()
for c in data:
print """
/**
* Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
* for <b><i>%(name)s</i></b>.
* <p>
* This constant holds the value of <b>{@value}</b>.
*
* @since 1.0
*
*/
public static final String %(code)s = \"%(code)s\";""" % c
where the data.txt file is a simple copy&paste from Wikipedia table (just remove all extra lines, making sure you have a country code and country name per line).
Then just place this into your static class:
/**
* Holds <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
* constant values for all countries.
*
* @since 1.0
*
* </p>
*/
public class CountryCode {
/**
* Constructor defined as <code>private</code> purposefully to ensure this
* class is only used to access its static properties and/or methods.
*/
private CountryCode() { }
/**
* Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
* for <b><i>Andorra</i></b>.
* <p>
* This constant holds the value of <b>{@value}</b>.
*
* @since 1.0
*
*/
public static final String AD = "AD";
//
// and the list goes on! ...
//
}
CountryCode enum was packaged into com.neovisionaries.i18n with other Java enums, LanguageCode (ISO 639-1), LanguageAlpha3Code (ISO 639-2), LocaleCode, ScriptCode (ISO 15924) and CurrencyCode (ISO 4217) and registered into the Maven Central Repository.
If you are already going to rely on Java locale, then I suggest using a simple HashMap instead of creating new classes for countries etc.
Here's how I would use it if I were to rely on the Java Localization only:
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());
}
After you fill the map, you can get the ISO code from the country name whenever you need it.
Or you can make it a ISO code to Country name map as well, just modify the 'put' method accordingly.
If anyone is already using the Amazon AWS SDK it includes com.amazonaws.services.route53domains.model.CountryCode. I know this is not ideal but it's an alternative if you already use the AWS SDK. For most cases I would use Takahiko's nv-i18n since, as he mentions, it implements ISO 3166-1.
Enum values are not hardcoded, but generated by a call to Locale.getISOCountries(). That is: Simply recompile the project against the newest java version to get any changes made to the list of countries reflected in the enum.
Con:
Not in Maven repository
Most likely simpler / less expressive than the other solutions, which I don't know.
Created for my own needs / not as such maintained. - You should probably clone the repo.