From 7786d72ea3b7cf7a1a7c7390aceb77a44bd5dae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=A6=82?= <8775@163.com> Date: Wed, 19 Feb 2025 16:11:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A0=E8=BD=BDi18n?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/message/OpenMessageFactory.java | 130 ++++-------------- 1 file changed, 26 insertions(+), 104 deletions(-) diff --git a/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/message/OpenMessageFactory.java b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/message/OpenMessageFactory.java index 4e9b3c82..034832a9 100755 --- a/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/message/OpenMessageFactory.java +++ b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/message/OpenMessageFactory.java @@ -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 walk = Files.walk(path)) { - Optional i18nRoot = walk.findFirst(); - if (!i18nRoot.isPresent()) { - return; + Resource[] resources = resolver.getResources(I18N_CLASSPATH); + Set 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 fileTrees = buildFileTree(root); - Set 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 buildIsvModuleList(List fileTreeList) { - if (fileTreeList == null) { - return Collections.emptySet(); - } - - Set isvModuleList = new HashSet<>(); - - List leafList = fileTreeList.stream().filter(FileTree::isLeaf).collect(Collectors.toList()); - for (FileTree fileTree : leafList) { - LinkedList nameList = new LinkedList<>(); - appendNames(nameList, fileTree); - String moduleName = String.join("/", nameList); - isvModuleList.add(moduleName); - } - - return isvModuleList; - } - - private static void appendNames(LinkedList nameList, FileTree fileTree) { - nameList.addFirst(fileTree.getName()); - FileTree parent = fileTree.getParent(); - if (parent != null) { - appendNames(nameList, parent); - } - } - - private static List buildFileTree(File file) { - List fileTrees = new ArrayList<>(); - appendFileTree(fileTrees, file, null); - return fileTrees; - } - - private static void appendFileTree(List 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 + '\'' + - '}'; - } - } - - }