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

io.quarkus.runtime.configuration.LocaleConverter Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
package io.quarkus.runtime.configuration;

import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY;

import java.io.Serializable;
import java.util.Locale;
import java.util.regex.Pattern;

import jakarta.annotation.Priority;

import org.eclipse.microprofile.config.spi.Converter;

/**
 * A converter to support locales.
 */
@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY)
public class LocaleConverter implements Converter, Serializable {

    private static final Pattern NORMALIZE_LOCALE_PATTERN = Pattern.compile("_");

    public LocaleConverter() {
    }

    @Override
    public Locale convert(final String value) {
        final String localeValue = value.trim();

        if (localeValue.isEmpty()) {
            return null;
        }

        if ("all".equals(localeValue)) {
            return Locale.ROOT;
        }

        Locale locale = Locale.forLanguageTag(NORMALIZE_LOCALE_PATTERN.matcher(localeValue).replaceAll("-"));
        if (locale != Locale.ROOT && (locale.getLanguage() == null || locale.getLanguage().isEmpty())) {
            throw new IllegalArgumentException("Unable to resolve locale: " + value);
        }

        return locale;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy