class LocaleUtil {

    public static Locale parseLocale(String localeString) {
        localeString = StringUtil.trimToNull(localeString);

        if (localeString == null) {
            return null;
        }

        String language = Emptys.EMPTY_STRING;
        String country = Emptys.EMPTY_STRING;
        String variant = Emptys.EMPTY_STRING;

        // language
        int start = 0;
        int index = localeString.indexOf("_");

        if (index >= 0) {
            language = localeString.substring(start, index).trim();

            // country
            start = index + 1;
            index = localeString.indexOf("_", start);

            if (index >= 0) {
                country = localeString.substring(start, index).trim();

                // variant
                variant = localeString.substring(index + 1).trim();
            } else {
                country = localeString.substring(start).trim();
            }
        } else {
            language = localeString.substring(start).trim();
        }

        return new Locale(language, country, variant);
    }

}