Compare commits

..

No commits in common. "912e11e6737daddf29620ad01dd44212b58fb6a5" and "c6ad66bb593c8e61b1c8e83e7590baddea9606c5" have entirely different histories.

4 changed files with 45 additions and 128 deletions

View File

@ -1,32 +0,0 @@
package cn.octopusyan.dmt.model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.File;
/**
* csv
*
* @author octopus_yan
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class WordCsvItem extends WordItem {
/**
* 开始下标(csv繁体
*/
private Integer indexTrad;
/**
* 原文(获取于csv繁体位置用于替换翻译文本
*/
private String originalTrad;
public WordCsvItem(File file, Integer lines, Integer index, String original, String chinese, Integer indexTrad, String originalTrad) {
super(file, lines, index, original, chinese);
this.indexTrad = indexTrad;
this.originalTrad = originalTrad;
}
}

View File

@ -34,8 +34,6 @@ public class WordItem {
*/ */
private StringProperty chineseProperty = new SimpleStringProperty(); private StringProperty chineseProperty = new SimpleStringProperty();
public WordItem() {}
public WordItem(File file, Integer lines, Integer index, String original, String chinese) { public WordItem(File file, Integer lines, Integer index, String original, String chinese) {
this.file = file; this.file = file;
this.lines = lines; this.lines = lines;

View File

@ -3,7 +3,6 @@ package cn.octopusyan.dmt.utils;
import cn.octopusyan.dmt.common.config.Constants; import cn.octopusyan.dmt.common.config.Constants;
import cn.octopusyan.dmt.common.config.Context; import cn.octopusyan.dmt.common.config.Context;
import cn.octopusyan.dmt.common.util.ProcessesUtil; import cn.octopusyan.dmt.common.util.ProcessesUtil;
import cn.octopusyan.dmt.model.WordCsvItem;
import cn.octopusyan.dmt.model.WordItem; import cn.octopusyan.dmt.model.WordItem;
import cn.octopusyan.dmt.view.ConsoleLog; import cn.octopusyan.dmt.view.ConsoleLog;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -175,32 +174,20 @@ public class PBOUtil {
try (LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name())) { try (LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name())) {
while (it.hasNext()) { while (it.hasNext()) {
lineIndex.addAndGet(1);
String line = it.next(); String line = it.next();
WordItem word = wordMap.get(lineIndex.get()); WordItem word = wordMap.get(lineIndex.get());
// 当前行是否有需要替换的文本 // 当前行是否有需要替换的文本
// TODO 是否替换空文本
if (word != null && line.contains(word.getOriginal())) { if (word != null && line.contains(word.getOriginal())) {
line = line.substring(0, word.getIndex()) +
if (word instanceof WordCsvItem csvItem) { line.substring(word.getIndex()).replace(word.getOriginal(), word.getChinese());
// 繁体部分
String trad = line.substring(csvItem.getIndexTrad(), csvItem.getIndex());
// 简体部分
String simp = line.substring(csvItem.getIndex());
// 拼接
line = line.substring(0, csvItem.getIndexTrad())
// Pattern.quote 处理转义字符
+ trad.replaceFirst(Pattern.quote(csvItem.getOriginalTrad()), csvItem.getChinese())
+ simp.replaceFirst(Pattern.quote(csvItem.getOriginal()), csvItem.getChinese());
} else {
line = line.substring(0, word.getIndex()) +
line.substring(word.getIndex()).replace(word.getOriginal(), word.getChinese());
}
} }
// 缓存行内容 // 缓存行内容
lines.add(line); lines.add(line);
lineIndex.addAndGet(1);
} }
} catch (IOException e) { } catch (IOException e) {
consoleLog.error(STR."文件[\{file.getAbsoluteFile()}]读取出错", e); consoleLog.error(STR."文件[\{file.getAbsoluteFile()}]读取出错", e);
@ -279,55 +266,25 @@ public class PBOUtil {
ArrayList<WordItem> wordItems = new ArrayList<>(); ArrayList<WordItem> wordItems = new ArrayList<>();
AtomicInteger lines = new AtomicInteger(0); AtomicInteger lines = new AtomicInteger(0);
int index = -1; int index = -1;
int indexTrad = -1;
int indexOriginal = -1;
String line; String line;
while (it.hasNext()) { while (it.hasNext()) {
line = it.next(); line = it.next();
boolean contains = line.contains("\""); List<String> split = Arrays.stream(line.split(",")).toList();
String delimit = contains ? "\",\"" : ",";
List<String> split = Arrays.stream(line.split(delimit)).toList();
lines.addAndGet(1);
if (lines.get() == 1) {
for (int i = 0; i < split.size(); i++) {
String colName = StringUtils.lowerCase(split.get(i));
if (colName.contains("original")) {
indexOriginal = i;
} else if (colName.contains("chinesesimp")) {
index = i;
} else if (colName.contains("chinese")) {
indexTrad = i;
}
}
continue;
}
if (index < split.size()) {
// 中文内容
String chinese = split.get(index).replaceAll("\",?", "");
// 已有中文翻译则跳过
if (containsChinese(chinese))
continue;
if (lines.get() == 0) {
index = split.indexOf("\"chinese\"");
} else if (index < split.size()) {
// 原文 // 原文
String original = split.get(indexOriginal).replaceAll("\"", ""); String original = split.get(index).replaceAll("\"", "");
// 繁体内容
String originalTrad = split.get(indexTrad).replaceAll("\"", "");
// 开始下标 // 开始下标
String searchSr = contains ? "\",\"" : ","; Integer startIndex = line.indexOf(original);
int startIndex = StringUtils.ordinalIndexOf(line, searchSr, index);
int startIndexTrad = StringUtils.ordinalIndexOf(line, searchSr, indexTrad);
// 如果带引号
startIndex += (contains ? 3 : 1);
startIndexTrad += (contains ? 3 : 1);
// 添加单词 // 添加单词
if (original.length() > 1) { if (original.length() > 1) {
wordItems.add(new WordCsvItem(file, lines.get(), startIndex, chinese, "", startIndexTrad, originalTrad)); wordItems.add(new WordItem(file, lines.get(), startIndex, original, ""));
} }
} }
lines.addAndGet(1);
} }
return wordItems; return wordItems;
} }
@ -345,17 +302,11 @@ public class PBOUtil {
String line; String line;
Matcher matcher; Matcher matcher;
while (it.hasNext()) { while (it.hasNext()) {
lines.addAndGet(1);
line = it.next(); line = it.next();
matcher = LAYOUT_PATTERN.matcher(line); matcher = LAYOUT_PATTERN.matcher(line);
if (StringUtils.isNoneEmpty(line) && matcher.matches()) {
if (StringUtils.isEmpty(line) || !matcher.matches()) // 原文
continue; String original = matcher.group(1);
// 原文
String original = matcher.group(1);
if (!original.startsWith("#")) {
// 开始下标 // 开始下标
Integer startIndex = line.indexOf(original); Integer startIndex = line.indexOf(original);
// 添加单词 // 添加单词
@ -363,6 +314,8 @@ public class PBOUtil {
wordItems.add(new WordItem(file, lines.get(), startIndex, original, "")); wordItems.add(new WordItem(file, lines.get(), startIndex, original, ""));
} }
} }
lines.addAndGet(1);
} }
return wordItems; return wordItems;
} }
@ -379,23 +332,24 @@ public class PBOUtil {
AtomicInteger lines = new AtomicInteger(0); AtomicInteger lines = new AtomicInteger(0);
while (it.hasNext()) { while (it.hasNext()) {
lines.addAndGet(1);
String line = it.next(); String line = it.next();
Matcher matcher = CPP_PATTERN.matcher(line); Matcher matcher = CPP_PATTERN.matcher(line);
if (!line.contains("$") && matcher.matches()) {
// 不匹配 是变量 则跳过 String name = matcher.group(1);
if (!matcher.matches() || line.contains("$") || line.contains("#"))
continue;
String name = matcher.group(1); // 原始文本
// 原始文本 int startIndex = line.indexOf(name) + name.length();
int startIndex = line.indexOf(name) + name.length(); String original = matcher.group(2);
String original = matcher.group(2);
// 添加单词 // 添加单词
if (original.length() > 1) { if (original.length() > 1) {
wordItems.add(new WordItem(file, lines.get(), startIndex, original, "")); wordItems.add(new WordItem(file, lines.get(), startIndex, original, ""));
}
} }
lines.addAndGet(1);
} }
return wordItems; return wordItems;
} }
@ -460,14 +414,4 @@ public class PBOUtil {
private static String outFilePath(File file, String suffix) { private static String outFilePath(File file, String suffix) {
return file.getParentFile().getAbsolutePath() + File.separator + FileUtil.mainName(file) + suffix; return file.getParentFile().getAbsolutePath() + File.separator + FileUtil.mainName(file) + suffix;
} }
/**
* 给定字符串是否含有中文
*
* @param str 需要判断的字符串
* @return 是否含有中文
*/
private static boolean containsChinese(String str) {
return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).find();
}
} }

View File

@ -14,7 +14,6 @@ import cn.octopusyan.dmt.view.ConsoleLog;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker; import javafx.concurrent.Worker;
import javafx.scene.control.ProgressIndicator; import javafx.scene.control.ProgressIndicator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -25,6 +24,7 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.DelayQueue; import java.util.concurrent.DelayQueue;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
/** /**
* 主界面 * 主界面
@ -103,7 +103,7 @@ public class MainViewModel extends BaseViewModel<MainController> {
* 开始翻译 * 开始翻译
*/ */
public void startTranslate() { public void startTranslate() {
if (wordItems.isEmpty()) return; if(wordItems.isEmpty()) return;
if (translateTask == null) { if (translateTask == null) {
List<WordItem> words = wordItems.stream().filter(item -> StringUtils.isEmpty(item.getChinese())).toList(); List<WordItem> words = wordItems.stream().filter(item -> StringUtils.isEmpty(item.getChinese())).toList();
@ -135,7 +135,7 @@ public class MainViewModel extends BaseViewModel<MainController> {
* 打包 * 打包
*/ */
public void pack() { public void pack() {
if (wordItems.isEmpty()) return; if(wordItems.isEmpty()) return;
PackTask packTask = new PackTask(wordItems, unpackPath); PackTask packTask = new PackTask(wordItems, unpackPath);
packTask.onListen(new PackTask.PackListener() { packTask.onListen(new PackTask.PackListener() {
@ -192,12 +192,19 @@ public class MainViewModel extends BaseViewModel<MainController> {
private void resetProgress() { private void resetProgress() {
translateTask = null; translateTask = null;
controller.translate.setGraphic(startIcon); controller.translate.setGraphic(startIcon);
ObservableList<String> styleClass = controller.translateProgress.getStyleClass(); Styles.toggleStyleClass(controller.translateProgress, Styles.SMALL);
if (!styleClass.contains(Styles.SMALL)) {
Styles.toggleStyleClass(controller.translateProgress, Styles.SMALL);
}
controller.translateProgress.progressProperty().unbind(); controller.translateProgress.progressProperty().unbind();
controller.translateProgress.setProgress(0); controller.translateProgress.setProgress(0);
controller.translateProgress.setVisible(false); controller.translateProgress.setVisible(false);
} }
/**
* 给定字符串是否含有中文
*
* @param str 需要判断的字符串
* @return 是否含有中文
*/
private boolean containsChinese(String str) {
return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).find();
}
} }