mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
修复加载i18n问题
This commit is contained in:
@@ -1,30 +1,22 @@
|
||||
package com.gitee.sop.support.message;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 负责构建错误消息
|
||||
@@ -36,7 +28,8 @@ public class OpenMessageFactory {
|
||||
static Logger logger = Logger.getLogger(OpenMessageFactory.class.getName());
|
||||
|
||||
private static final String SOLUTION = ".solution";
|
||||
private static final String I18N_ROOT = "i18n";
|
||||
private static final String I18N_FOLDER = "i18n";
|
||||
private static final String I18N_CLASSPATH = "classpath*:" + I18N_FOLDER + "/**/*.properties";
|
||||
|
||||
private OpenMessageFactory() {
|
||||
}
|
||||
@@ -52,89 +45,35 @@ public class OpenMessageFactory {
|
||||
private static MessageSourceAccessor errorMessageSourceAccessor;
|
||||
|
||||
public static void initMessage() {
|
||||
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
URL i18nFolder = contextClassLoader.getResource(I18N_ROOT);
|
||||
if (i18nFolder == null) {
|
||||
return;
|
||||
}
|
||||
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
|
||||
try {
|
||||
Path path = Paths.get(i18nFolder.toURI());
|
||||
try (Stream<Path> walk = Files.walk(path)) {
|
||||
Optional<Path> i18nRoot = walk.findFirst();
|
||||
if (!i18nRoot.isPresent()) {
|
||||
return;
|
||||
Resource[] resources = resolver.getResources(I18N_CLASSPATH);
|
||||
Set<String> isvModuleList = new LinkedHashSet<>();
|
||||
logger.info("resource size:" + resources.length);
|
||||
for (Resource resource : resources) {
|
||||
String path = resource.getURL().getPath();
|
||||
if (path == null) {
|
||||
continue;
|
||||
}
|
||||
File root = i18nRoot.get().toFile();
|
||||
List<FileTree> fileTrees = buildFileTree(root);
|
||||
Set<String> isvModuleList = buildIsvModuleList(fileTrees);
|
||||
OpenMessageFactory.initMessageSource(new ArrayList<>(isvModuleList));
|
||||
int j = path.lastIndexOf(I18N_FOLDER);
|
||||
// i18n/open/error_zh_CN.properties
|
||||
String shortPath = path.substring(j);
|
||||
int i = shortPath.indexOf("_");
|
||||
if (i < 0) {
|
||||
continue;
|
||||
}
|
||||
String module = shortPath.substring(0, i);
|
||||
// i18n/open/code,i18n/open/error
|
||||
isvModuleList.add(module);
|
||||
}
|
||||
} catch (URISyntaxException | IOException e) {
|
||||
OpenMessageFactory.initMessageSource(new ArrayList<>(isvModuleList));
|
||||
} catch (IOException e) {
|
||||
logger.warning("初始化i18n模块错误:" + e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> buildIsvModuleList(List<FileTree> fileTreeList) {
|
||||
if (fileTreeList == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Set<String> isvModuleList = new HashSet<>();
|
||||
|
||||
List<FileTree> leafList = fileTreeList.stream().filter(FileTree::isLeaf).collect(Collectors.toList());
|
||||
for (FileTree fileTree : leafList) {
|
||||
LinkedList<String> nameList = new LinkedList<>();
|
||||
appendNames(nameList, fileTree);
|
||||
String moduleName = String.join("/", nameList);
|
||||
isvModuleList.add(moduleName);
|
||||
}
|
||||
|
||||
return isvModuleList;
|
||||
}
|
||||
|
||||
private static void appendNames(LinkedList<String> nameList, FileTree fileTree) {
|
||||
nameList.addFirst(fileTree.getName());
|
||||
FileTree parent = fileTree.getParent();
|
||||
if (parent != null) {
|
||||
appendNames(nameList, parent);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<FileTree> buildFileTree(File file) {
|
||||
List<FileTree> fileTrees = new ArrayList<>();
|
||||
appendFileTree(fileTrees, file, null);
|
||||
return fileTrees;
|
||||
}
|
||||
|
||||
private static void appendFileTree(List<FileTree> fileTrees, File file, FileTree parent) {
|
||||
FileTree i18nTree = new FileTree();
|
||||
i18nTree.setParent(parent);
|
||||
|
||||
fileTrees.add(i18nTree);
|
||||
if (file.isDirectory()) {
|
||||
i18nTree.setName(file.getName());
|
||||
File[] files = file.listFiles();
|
||||
if (files == null || files.length == 0) {
|
||||
return;
|
||||
}
|
||||
for (File childFile : files) {
|
||||
appendFileTree(fileTrees, childFile, i18nTree);
|
||||
}
|
||||
} else {
|
||||
// i18n/isp/bizerror_en.properties
|
||||
String name = file.getName();
|
||||
int i = name.indexOf("_");
|
||||
if (i < 0) {
|
||||
return;
|
||||
}
|
||||
String module = name.substring(0, i);
|
||||
|
||||
i18nTree.setName(module);
|
||||
i18nTree.setLeaf(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置国际化资源信息
|
||||
@@ -206,21 +145,4 @@ public class OpenMessageFactory {
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class FileTree {
|
||||
private String name;
|
||||
|
||||
private FileTree parent;
|
||||
|
||||
private boolean isLeaf;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FileTree{" +
|
||||
"name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user