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

com.nls.util.Locales Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.nls.util;

import com.google.common.base.Strings;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public final class Locales {
    private static LocaleResolver defaultResolver = new LocaleResolver("en");

    private Locales() {

    }

    /**
     * @deprecated legacy method
     */
    @Deprecated
    public static List get() {
        return defaultResolver.getLocales();
    }

    /**
     * @deprecated legacy method
     */
    @Deprecated
    public static LocaleResolver getDefaultResolver() {
        return defaultResolver;
    }

    /**
     * @deprecated legacy method
     */
    @Deprecated
    public static void setDefaultResolver(LocaleResolver resolver) {
        defaultResolver = resolver;
    }

    /**
     * @deprecated legacy method
     */
    @Deprecated
    public static Locale resolve(String acceptLanguage, Locale defaultLocale) {
        return defaultResolver.resolve(acceptLanguage, defaultLocale);
    }

    public static List forLanguageTags(String... languageTags) {
        return Arrays.stream(languageTags)
                .map(Locales::forLanguageTag)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public static List forLanguageTags(String languageTags, String separator) {
        return forLanguageTags(languageTags.split(separator));
    }

    public static Locale forLanguageTag(String tag) {
        String normalised = Strings.nullToEmpty(tag).trim().replaceAll("_", "-");
        return tag == null || normalised.isEmpty() ? null : Locale.forLanguageTag(normalised);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy