All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.util.convert.LocaleConversions Maven / Gradle / Ivy

There is a newer version: 2.18.0
Show newest version
package com.cedarsoftware.util.convert;

import java.util.Locale;
import java.util.Map;

import com.cedarsoftware.util.CompactLinkedMap;
import com.cedarsoftware.util.StringUtilities;

import static com.cedarsoftware.util.convert.MapConversions.COUNTRY;
import static com.cedarsoftware.util.convert.MapConversions.LANGUAGE;
import static com.cedarsoftware.util.convert.MapConversions.SCRIPT;
import static com.cedarsoftware.util.convert.MapConversions.VARIANT;

public final class LocaleConversions {
    private LocaleConversions() {}

    static String toString(Object from, Converter converter) {
        return ((Locale)from).toLanguageTag();
    }

    static Map toMap(Object from, Converter converter) {
        Locale locale = (Locale) from;
        Map map = new CompactLinkedMap<>();

        String language = locale.getLanguage();
        map.put(LANGUAGE, language);

        String country = locale.getCountry();
        if (StringUtilities.hasContent(country)) {
            map.put(COUNTRY, country);
        }

        String script = locale.getScript();
        if (StringUtilities.hasContent(script)) {
            map.put(SCRIPT, script);
        }

        String variant = locale.getVariant();
        if (StringUtilities.hasContent(variant)) {
            map.put(VARIANT, variant);
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy