Codify country name

Veröffentlicht von Marco Betschart am

import java.util.Locale;

/**
Codifies country names to their ISO country code and vice versa:
    - LocaleCountry.nameOf("CH", new Locale("en")) -> "Switzerland"
    - LocaleCountry.codeOf("Schweiz", new Locale("de")) -> "CH"
    - LocaleCountry.codeExists("CH") -> true
*/
public class LocaleCountry {
    
    public static String nameOf( String code ){
        return LocaleCountry.nameOf( code, Locale.getDefault() );
    }


    public static String nameOf( String code, Locale locale ){
        return new Locale(locale.getLanguage(),code.toUpperCase()).getDisplayCountry(locale);
    }
    
    
    public static String codeOf( String name ){
        Locale sys = Locale.getDefault();
        String code = LocaleCountry.codeOf(name, sys);
        
        if( code != null ){
            return code;
            
        } else if( !sys.getLanguage().equals("en") ){
            return LocaleCountry.codeOf(name,new Locale("en"));
        }
        
        return null;
    }
    
    
    public static String codeOf( String name, Locale locale ){
        String[] codes = Locale.getISOCountries();
        
        for( String code: codes ){
            if( name.equalsIgnoreCase(LocaleCountry.nameOf(code, locale)) ){
                return code;
            }
        }
        
        return null;
    }
    
    
    public static String codeOf( String name, Locale[] locales ){
        for( Locale locale: locales ){
            String code = LocaleCountry.codeOf(name, locale);
            
            if( code != null ){
                return code;
            }
        }
        
        return null;
    }
    
    
    public static boolean codeExists( String code ){
        String[] codes = Locale.getISOCountries();
        
        for( String comparison: codes ){
            if( code.equalsIgnoreCase(comparison) ){
                return true;
            }
        }
        
        return false;
    }
}
Kategorien: Technologie

Marco Betschart

nerdpreneur out of conviction. lover of the never ending journey of exploration. self growth and tech enthusiast. ambitious rock climber and from time to time, adrenaline junkie.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.