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

org.zodiac.autoconfigure.web.ServletMessageLocaleAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.web;

import java.util.Locale;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered;
import org.zodiac.autoconfigure.context.MessageSourceConfigProperties;
import org.zodiac.commons.constants.Constants;
import org.zodiac.commons.util.ObjectUtil;

@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
@AutoConfigureOrder(value = Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(value = {javax.servlet.Servlet.class, org.springframework.web.servlet.DispatcherServlet.class})
public class ServletMessageLocaleAutoConfiguration {

    private final WebMvcProperties webMvcProperties;

    public ServletMessageLocaleAutoConfiguration(WebMvcProperties webMvcProperties) {
        this.webMvcProperties = webMvcProperties;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public org.springframework.web.servlet.LocaleResolver localeResolver(MessageSource messageSource,
        MessageSourceConfigProperties messageSourceConfigProperties) {
        Locale defaultLocale = ObjectUtil.defaultIfNull(webMvcProperties.getLocale(), Constants.Zodiac.DEFAULT_LOCALE);
        LocaleContextHolder.setDefaultLocale(defaultLocale);

        org.zodiac.core.web.servlet.ServletLocaleResolver resolver =
            new org.zodiac.core.web.servlet.ServletLocaleResolver(messageSource);
        resolver.setDefaultLocale(defaultLocale)
            .setLocaleAttributeName(messageSourceConfigProperties.getLocale().getLocaleKey())
            .setLocaleAdditionalAttributeNames(messageSourceConfigProperties.getLocale().getLocaleAdditionalKeys())
            .setTimeZoneAttributeName(messageSourceConfigProperties.getLocale().getTimeZoneKey());
        return resolver;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy