org.zodiac.autoconfigure.web.ReactiveMessageLocaleAutoConfiguration Maven / Gradle / Ivy
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.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.REACTIVE)
@AutoConfigureOrder(value = Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(value = {org.springframework.web.server.WebHandler.class, org.springframework.web.reactive.DispatcherHandler.class})
public class ReactiveMessageLocaleAutoConfiguration {
private final WebFluxConfigProperties webFluxConfigProperties;
public ReactiveMessageLocaleAutoConfiguration(WebFluxConfigProperties webFluxConfigProperties) {
this.webFluxConfigProperties = webFluxConfigProperties;
}
@Bean
@Primary
@ConditionalOnMissingBean
public org.springframework.web.server.i18n.LocaleContextResolver localeResolver(MessageSource messageSource,
MessageSourceConfigProperties messageSourceConfigProperties) {
Locale defaultLocale = ObjectUtil.defaultIfNull(webFluxConfigProperties.getLocale(), Constants.Zodiac.DEFAULT_LOCALE);
LocaleContextHolder.setDefaultLocale(defaultLocale);
org.zodiac.core.web.reactive.ReactiveLocaleResolver resolver =
new org.zodiac.core.web.reactive.ReactiveLocaleResolver(messageSource);
resolver.setDefaultLocale(defaultLocale)
.setLocaleAttributeName(messageSourceConfigProperties.getLocale().getLocaleKey())
.setLocaleAdditionalAttributeNames(messageSourceConfigProperties.getLocale().getLocaleAdditionalKeys())
.setTimeZoneAttributeName(messageSourceConfigProperties.getLocale().getTimeZoneKey());
return resolver;
}
}