public static String getCountryISO2 ( String country ) { country = StringUtils . trimToNull ( country ) ; if ( country == null ) return null ; if ( country . length ( ) == 2 ) return country ; String [ ] iso2Codes = Locale . getISOCountries ( ) ; if ( country . length ( ) == 3 ) { String iso2Code = LocaleUtils . getCountryISO2FromISO3 ( country ) ; if ( iso2Code != null ) return iso2Code ; } for ( String iso2Code : iso2Codes ) { Locale countryLocale = new Locale ( iso2Code , iso2Code ) ; for ( Locale translationLocale : LocaleUtils . availableLocaleList ( ) ) { String name = StringUtils . trimToNull ( countryLocale . getDisplayCountry ( translationLocale ) ) ; if ( name != null && name . equalsIgnoreCase ( country ) ) return iso2Code ; } } return null ; }
Return an ISO - 2 country code from a country name .