🔨 perf(i18n): 替换 i18n key 先写入内存再判断是否变动后写入文件

This commit is contained in:
小吾立 2024-06-14 12:57:25 +08:00
parent f4f8367b9d
commit e1ed6614a5
6 changed files with 32 additions and 50 deletions

View File

@ -255,7 +255,7 @@ public class JpomApplication implements DisposableBean, InitializingBean {
public static void shutdownGlobalThreadPool() { public static void shutdownGlobalThreadPool() {
LINK_EXECUTOR_SERVICE.forEach((s, executorService) -> { LINK_EXECUTOR_SERVICE.forEach((s, executorService) -> {
if (!executorService.isShutdown()) { if (!executorService.isShutdown()) {
log.debug("shutdown {} ThreadPool", s); log.debug(I18nMessageUtil.get("i18n.close_thread_pool.4cd9"), s);
executorService.shutdownNow(); executorService.shutdownNow();
} }
}); });

View File

@ -1,5 +1,5 @@
#i18n en #i18n en
#Fri Jun 14 12:12:55 CST 2024 #Fri Jun 14 12:53:47 CST 2024
i18n.ssh_info_does_not_exist.5ed0=SSH information does not exist i18n.ssh_info_does_not_exist.5ed0=SSH information does not exist
i18n.incompatible_program_versions.5291=The current program version {} The new version of the program is minimum compatible {} and cannot be upgraded directly i18n.incompatible_program_versions.5291=The current program version {} The new version of the program is minimum compatible {} and cannot be upgraded directly
i18n.no_projects_configured.e873=No items are configured i18n.no_projects_configured.e873=No items are configured
@ -1051,6 +1051,7 @@ i18n.unsupported_mode_with_script_log.6a7a=Unsupported mode, script log
i18n.ssh_connection_failed.74ab=SSH connection failed, please check whether the username, password, host, port, etc. are filled in correctly, and whether the timeout time is reasonable\: i18n.ssh_connection_failed.74ab=SSH connection failed, please check whether the username, password, host, port, etc. are filled in correctly, and whether the timeout time is reasonable\:
i18n.node_name_required.5bdf=Please fill in the node name i18n.node_name_required.5bdf=Please fill in the node name
i18n.client_terminated_connection.6886=Client side terminates connection\: {} i18n.client_terminated_connection.6886=Client side terminates connection\: {}
i18n.close_thread_pool.4cd9=Close the {} thread pool
i18n.no_corresponding_folder.621f=No corresponding folder i18n.no_corresponding_folder.621f=No corresponding folder
i18n.no_online_manager_node_found.05d7=The current cluster does not find an online management node i18n.no_online_manager_node_found.05d7=The current cluster does not find an online management node
i18n.command_execution_failed.90ef=Command execution failed i18n.command_execution_failed.90ef=Command execution failed

View File

@ -1,5 +1,5 @@
#i18n zh #i18n zh
#Fri Jun 14 12:12:44 CST 2024 #Fri Jun 14 12:53:46 CST 2024
i18n.ssh_info_does_not_exist.5ed0=ssh 信息不存在啦 i18n.ssh_info_does_not_exist.5ed0=ssh 信息不存在啦
i18n.incompatible_program_versions.5291=当前程序版本 {} 新版程序最低兼容 {} 不能直接升级 i18n.incompatible_program_versions.5291=当前程序版本 {} 新版程序最低兼容 {} 不能直接升级
i18n.no_projects_configured.e873=没有配置任何项目 i18n.no_projects_configured.e873=没有配置任何项目
@ -1051,6 +1051,7 @@ i18n.unsupported_mode_with_script_log.6a7a=不支持的模式script log
i18n.ssh_connection_failed.74ab=ssh连接失败,请检查用户名、密码、host、端口等填写是否正确超时时间是否合理 i18n.ssh_connection_failed.74ab=ssh连接失败,请检查用户名、密码、host、端口等填写是否正确超时时间是否合理
i18n.node_name_required.5bdf=请填写节点名称 i18n.node_name_required.5bdf=请填写节点名称
i18n.client_terminated_connection.6886=客户端终止连接:{} i18n.client_terminated_connection.6886=客户端终止连接:{}
i18n.close_thread_pool.4cd9=关闭 {} 线程池
i18n.no_corresponding_folder.621f=没有对应文件夹 i18n.no_corresponding_folder.621f=没有对应文件夹
i18n.no_online_manager_node_found.05d7=当前集群未找到在线的管理节点 i18n.no_online_manager_node_found.05d7=当前集群未找到在线的管理节点
i18n.command_execution_failed.90ef=执行命令失败 i18n.command_execution_failed.90ef=执行命令失败

View File

@ -176,6 +176,7 @@
"i18n.close_session_exception.3491":"关闭会话异常", "i18n.close_session_exception.3491":"关闭会话异常",
"i18n.close_session_exception_with_detail.85f0":"关闭会话异常:{}", "i18n.close_session_exception_with_detail.85f0":"关闭会话异常:{}",
"i18n.close_success.8a31":"关闭成功", "i18n.close_success.8a31":"关闭成功",
"i18n.close_thread_pool.4cd9":"关闭 {} 线程池",
"i18n.cloud_server_network_issues.a865":"云服务器的安全组配置等网络相关问题排查定位。", "i18n.cloud_server_network_issues.a865":"云服务器的安全组配置等网络相关问题排查定位。",
"i18n.cluster_address_check_exception.cd92":"填写的集群地址检查异常,请确认集群地址是正确的服务端地址,", "i18n.cluster_address_check_exception.cd92":"填写的集群地址检查异常,请确认集群地址是正确的服务端地址,",
"i18n.cluster_binding_success.eb7e":"集群绑定成功", "i18n.cluster_binding_success.eb7e":"集群绑定成功",

View File

@ -29,12 +29,8 @@ import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.io.BufferedReader; import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -232,22 +228,16 @@ public class ExtractI18nTest {
} }
// 代码中已经使用到的 key // 代码中已经使用到的 key
Collection<Object> useKeys = new HashSet<>(); Collection<Object> useKeys = new HashSet<>();
// 临时文件
File tempDir = FileUtil.file(rootFile, "i18n-temp");
// 删除临时文件
FileUtil.del(tempDir);
// 替换中文 // 替换中文
walkFile(rootFile, file1 -> { walkFile(rootFile, file1 -> {
try { try {
for (Pattern chinesePattern : chinesePatterns) { for (Pattern chinesePattern : chinesePatterns) {
replaceQuotedChineseInFile(file1, tempDir, chinesePattern, chineseMap, useKeys); replaceQuotedChineseInFile(file1, chinesePattern, chineseMap, useKeys);
} }
} catch (Exception e) { } catch (Exception e) {
throw Lombok.sneakyThrow(e); throw Lombok.sneakyThrow(e);
} }
}); });
// 删除临时文件
FileUtil.del(tempDir);
// //
boolean isChange = false; boolean isChange = false;
for (Object keyObj : CollUtil.newArrayList(zhProperties.keySet())) { for (Object keyObj : CollUtil.newArrayList(zhProperties.keySet())) {
@ -397,11 +387,11 @@ public class ExtractI18nTest {
// 变动才保存 // 变动才保存
// 根据 key 排序 // 根据 key 排序
TreeMap<String, Object> sort = MapUtil.sort(updateAfter); TreeMap<String, Object> sort = MapUtil.sort(updateAfter);
FileUtil.writeString(JSONArray.toJSONString(sort, JSONWriter.Feature.PrettyFormat), wordsFile, StandardCharsets.UTF_8); FileUtil.writeString(JSONArray.toJSONString(sort, JSONWriter.Feature.PrettyFormat), wordsFile, charset);
} }
} else { } else {
TreeMap<String, Object> sort = MapUtil.sort(jsonObject); TreeMap<String, Object> sort = MapUtil.sort(jsonObject);
FileUtil.writeString(JSONArray.toJSONString(sort, JSONWriter.Feature.PrettyFormat), wordsFile, StandardCharsets.UTF_8); FileUtil.writeString(JSONArray.toJSONString(sort, JSONWriter.Feature.PrettyFormat), wordsFile, charset);
} }
} }
@ -465,7 +455,7 @@ public class ExtractI18nTest {
return; return;
} }
String path = FileUtil.getAbsolutePath(file1); String path = FileUtil.getAbsolutePath(file1);
if (StrUtil.containsAny(path, "/test/", "/i18n-temp/", "\\test\\", "\\i18n-temp\\")) { if (StrUtil.containsAny(path, "/test/", "\\test\\")) {
return; return;
} }
if (StrUtil.equals("java", FileUtil.extName(file1))) { if (StrUtil.equals("java", FileUtil.extName(file1))) {
@ -482,14 +472,12 @@ public class ExtractI18nTest {
* @param pattern 当前匹配的正则 * @param pattern 当前匹配的正则
* @throws IOException io 异常 * @throws IOException io 异常
*/ */
private void replaceQuotedChineseInFile(File file, File tempDir, Pattern pattern, Map<String, String> chineseMap, Collection<Object> useKeys) throws Exception { private void replaceQuotedChineseInFile(File file, Pattern pattern, Map<String, String> chineseMap, Collection<Object> useKeys) throws Exception {
String subPath = FileUtil.subPath(rootFile.getAbsolutePath(), file); //String subPath = FileUtil.subPath(rootFile.getAbsolutePath(), file);
// 先存储于临时文件 // 先存储于临时文件
File tempFile = FileUtil.file(tempDir, subPath);
FileUtil.mkParentDirs(tempFile);
boolean modified = false; boolean modified = false;
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8); StringWriter writer = new StringWriter();
BufferedWriter writer = Files.newBufferedWriter(tempFile.toPath())) { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), charset)) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (canIgnore(line)) { if (canIgnore(line)) {
@ -538,16 +526,16 @@ public class ExtractI18nTest {
throw new IllegalStateException("替换后成为忽略行:" + line + " \n" + lineString); throw new IllegalStateException("替换后成为忽略行:" + line + " \n" + lineString);
} }
writer.write(lineString); writer.write(lineString);
modified = true; if (!modified) {
modified = !StrUtil.equals(line, lineString);
}
} }
writer.newLine(); writer.write(FileUtil.getLineSeparator());
} }
} }
if (modified) { if (modified) {
// 移动到原路径 // 移动到原路径
FileUtil.move(tempFile, file, true); FileUtil.writeString(writer.toString(), file, charset);
} else {
FileUtil.del(tempFile);
} }
} }

View File

@ -16,12 +16,8 @@ import lombok.Lombok;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.BufferedReader; import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Properties; import java.util.Properties;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -53,13 +49,11 @@ public class RestoreI18nTest {
try (BufferedReader inputStream = FileUtil.getReader(zhPropertiesFile, charset)) { try (BufferedReader inputStream = FileUtil.getReader(zhPropertiesFile, charset)) {
zhProperties.load(inputStream); zhProperties.load(inputStream);
} }
// 临时文件
File tempDir = FileUtil.file(rootFile, "i18n-temp");
// 提取中文 // 提取中文
ExtractI18nTest.walkFile(rootFile, file1 -> { ExtractI18nTest.walkFile(rootFile, file1 -> {
try { try {
for (Pattern pattern : ExtractI18nTest.messageKeyPatterns) { for (Pattern pattern : ExtractI18nTest.messageKeyPatterns) {
restoreChineseInFile(file1, tempDir, pattern, zhProperties); restoreChineseInFile(file1, pattern, zhProperties);
} }
} catch (Exception e) { } catch (Exception e) {
throw Lombok.sneakyThrow(e); throw Lombok.sneakyThrow(e);
@ -67,14 +61,10 @@ public class RestoreI18nTest {
}); });
} }
private void restoreChineseInFile(File file, File tempDir, Pattern pattern, Properties zhProperties) throws Exception { private void restoreChineseInFile(File file, Pattern pattern, Properties zhProperties) throws Exception {
String subPath = FileUtil.subPath(rootFile.getAbsolutePath(), file); StringWriter writer = new StringWriter();
// 先存储于临时文件
File tempFile = FileUtil.file(tempDir, subPath);
FileUtil.mkParentDirs(tempFile);
boolean modified = false; boolean modified = false;
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8); try (BufferedReader reader = Files.newBufferedReader(file.toPath(), charset)) {
BufferedWriter writer = Files.newBufferedWriter(tempFile.toPath())) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (ExtractI18nTest.canIgnore(line)) { if (ExtractI18nTest.canIgnore(line)) {
@ -114,17 +104,18 @@ public class RestoreI18nTest {
} }
matcher.appendTail(modifiedLine); matcher.appendTail(modifiedLine);
} }
writer.write(modifiedLine.toString()); String lineString = modifiedLine.toString();
modified = true; writer.write(lineString);
if (!modified) {
modified = !StrUtil.equals(line, lineString);
}
} }
writer.newLine(); writer.write(FileUtil.getLineSeparator());
} }
} }
if (modified) { if (modified) {
// 移动到原路径 // 移动到原路径
FileUtil.move(tempFile, file, true); FileUtil.writeString(writer.toString(), file, charset);
} else {
FileUtil.del(tempFile);
} }
} }
} }