package com.qianwen.core.i18n.config;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import java.time.Duration;
|
import java.util.ArrayList;
|
import java.util.List;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import com.qianwen.core.i18n.source.WildcardReloadableResourceBundleMessageSource;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.context.MessageSourceProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.context.MessageSource;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Primary;
|
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
import org.springframework.util.StringUtils;
|
|
@EnableConfigurationProperties
|
@AutoConfigureBefore({MessageSourceAutoConfiguration.class})
|
@Configuration(proxyBeanMethods = false)
|
@ConditionalOnMissingBean(name = {"messageSource"}, search = SearchStrategy.CURRENT)
|
@AutoConfigureOrder(Integer.MIN_VALUE)
|
@Conditional({CustomMessageSourceAutoConfiguration.ResourceBundleCondition.class})
|
/* loaded from: blade-starter-i18n-9.3.0.0-SNAPSHOT.jar:org/springblade/core/i18n/config/CustomMessageSourceAutoConfiguration.class */
|
public class CustomMessageSourceAutoConfiguration {
|
private static final Logger log = LoggerFactory.getLogger(CustomMessageSourceAutoConfiguration.class);
|
private static final Resource[] NO_RESOURCES = new Resource[0];
|
|
@ConfigurationProperties(prefix = "spring.messages")
|
@ConditionalOnMissingBean
|
@Bean
|
public MessageSourceProperties messageSourceProperties() {
|
return new MessageSourceProperties();
|
}
|
|
@Bean
|
@Primary
|
public MessageSource messageSource(MessageSourceProperties properties) {
|
WildcardReloadableResourceBundleMessageSource messageSource = new WildcardReloadableResourceBundleMessageSource();
|
if (StringUtils.hasText(properties.getBasename())) {
|
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
|
}
|
if (properties.getEncoding() != null) {
|
messageSource.setDefaultEncoding(properties.getEncoding().name());
|
}
|
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
|
Duration cacheDuration = properties.getCacheDuration();
|
if (cacheDuration != null) {
|
messageSource.setCacheMillis(cacheDuration.toMillis());
|
}
|
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
|
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
|
return messageSource;
|
}
|
|
/* loaded from: blade-starter-i18n-9.3.0.0-SNAPSHOT.jar:org/springblade/core/i18n/config/CustomMessageSourceAutoConfiguration$ResourceBundleCondition.class */
|
protected static class ResourceBundleCondition extends SpringBootCondition {
|
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
|
|
protected ResourceBundleCondition() {
|
}
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
|
ConditionOutcome outcome = (ConditionOutcome) cache.get(basename);
|
if (outcome == null) {
|
outcome = getMatchOutcomeForBasename(context, basename);
|
cache.put(basename, outcome);
|
}
|
return outcome;
|
}
|
|
private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
|
String[] commaDelimitedListToStringArray;
|
Resource[] resources;
|
ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle", new Object[0]);
|
for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
|
List<Resource> resourceList = new ArrayList<>();
|
for (Resource resource : getResources(context.getClassLoader(), name)) {
|
if (resource.exists()) {
|
CustomMessageSourceAutoConfiguration.log.info("扫描到的包路径" + resource.getFilename());
|
resourceList.add(resource);
|
}
|
}
|
if (CollectionUtil.isNotEmpty(resourceList)) {
|
return ConditionOutcome.match(message.found("bundle").items(resourceList));
|
}
|
}
|
return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
|
}
|
|
private Resource[] getResources(ClassLoader classLoader, String name) {
|
String target = name.replace('.', '/');
|
try {
|
return new PathMatchingResourcePatternResolver(classLoader).getResources("classpath*:" + target + ".properties");
|
} catch (Exception e) {
|
return CustomMessageSourceAutoConfiguration.NO_RESOURCES;
|
}
|
}
|
}
|
}
|