|
@@ -1,6 +1,7 @@
|
|
|
package org.hswebframework.web.starter.i18n;
|
|
|
|
|
|
-import org.hswebframework.web.exception.BusinessException;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.hswebframework.web.i18n.MessageSourceInitializer;
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
|
@@ -10,20 +11,35 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
|
|
import org.springframework.core.Ordered;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
|
|
|
+@Slf4j
|
|
|
public class I18nConfiguration {
|
|
|
|
|
|
-
|
|
|
@Bean
|
|
|
- public MessageSource coreMessageSource(){
|
|
|
- ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();
|
|
|
+ @SneakyThrows
|
|
|
+ public MessageSource autoResolveI18nMessageSource() {
|
|
|
+
|
|
|
+ ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
|
|
messageSource.setDefaultEncoding("UTF-8");
|
|
|
- messageSource.setBundleClassLoader(BusinessException.class.getClassLoader());
|
|
|
- messageSource.setBasenames("i18n/core/messages");
|
|
|
+ Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:i18n/**");
|
|
|
+
|
|
|
+ for (Resource resource : resources) {
|
|
|
+ String path = resource.getURL().getPath();
|
|
|
+ if (StringUtils.hasText(path) && (path.endsWith(".properties") || path.endsWith(".xml"))) {
|
|
|
+ String name = path.substring(path.lastIndexOf("i18n"),path.indexOf("_"));
|
|
|
+
|
|
|
+ log.info("register i18n message resource {} -> {}", path,name);
|
|
|
+
|
|
|
+ messageSource.addBasenames(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
return messageSource;
|
|
|
}
|
|
|
|