Skip to content

Commit dbc1376

Browse files
V3.6
1.支持多语言文件的处理
1 parent 870552c commit dbc1376

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

src/gui/Displayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void initComponents(){
3939
javax.swing.JTextField jTextField1 = new javax.swing.JTextField();
4040

4141
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
42-
String version = "V3.5";
42+
String version = "V3.6";
4343
setTitle(getI18nText("gui.title") + version);
4444
setIconImage(new ImageIcon(getClass().getResource("/assets/icon.jpg")).getImage());
4545
jTextField1.setEditable(false);

src/io/BATFileVisitor.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.Items.LocalizableItem;
44

5+
import java.io.File;
56
import java.io.IOException;
67
import java.nio.file.*;
78
import java.nio.file.attribute.BasicFileAttributes;
@@ -24,7 +25,7 @@ public FileVisitResult visitFileFailed(Path file, IOException exc) {
2425
}
2526

2627
public static LinkedList<LocalizableItem> visit(Path path) throws IOException {
27-
logger.setLevel(Level.OFF);
28+
logger.setLevel(Level.INFO);
2829
if (visitor == null) {
2930
visitor = new BATFileVisitor();
3031
}
@@ -34,15 +35,26 @@ public static LinkedList<LocalizableItem> visit(Path path) throws IOException {
3435
return visitor.itemList;
3536
}
3637

37-
private void fillList(List<String> info, String type, List<String> keys) {
38+
private List<String> findOtherLang(Path path) throws IOException {
39+
Path infoFile = path.subpath(path.getNameCount() - 2, path.getNameCount() - 1);
40+
File[] files = path.toFile().getParentFile().listFiles(((dir, name) -> name.endsWith(".dat") && !name.endsWith("English.dat") && !name.endsWith(infoFile + ".dat")));
41+
if (files != null && files.length != 0) {
42+
logger.info("Found Other Language File:" + files[0]);
43+
return Files.readAllLines(files[0].toPath());
44+
}
45+
else
46+
return new ArrayList<>();
47+
}
48+
49+
private HashMap<String, String> toMap(List<String> list) {
3850
HashMap<String, String> infoMap = new HashMap<>();
39-
info.forEach(x -> {
51+
list.forEach(x -> {
4052
//只分割一次
4153
String[] result = x.split(" ", 2);
4254
if (result.length == 2)
4355
infoMap.put(result[0], result[1]);
4456
});
45-
itemList.add(new LocalizableItem(type, keys, infoMap));
57+
return infoMap;
4658
}
4759

4860
@Override
@@ -61,7 +73,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
6173
List<String> info = new ArrayList<>();
6274
String path = file.toString();
6375
info.add("Path" + " " + path);
64-
logger.info("Found Language File:" + file);
76+
logger.info("Found English Language File:" + file);
6577
//语言文件位于的文件夹名称
6678
Path pathname = file.subpath(file.getNameCount() - 2, file.getNameCount() - 1);
6779
//用于一般描述的描述文件判断——物品、物体、载具等等
@@ -72,16 +84,16 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
7284
info.addAll(Files.readAllLines(file));
7385
//物品
7486
if (path.contains("Item")) {
75-
fillList(info, Item.getName(), Item.getAttrs());
87+
itemList.add(new LocalizableItem(Item.getName(), Item.getAttrs(), toMap(info), toMap(findOtherLang(file))));
7688
}//物体
7789
else if (path.contains("Objects") || path.contains("Tree")) {
78-
fillList(info, Objects.getName(), Objects.getAttrs());
90+
itemList.add(new LocalizableItem(Objects.getName(), Objects.getAttrs(), toMap(info), toMap(findOtherLang(file))));
7991
}//载具
8092
else if (path.contains("Vehicle")) {
81-
fillList(info, Vehicle.getName(), Vehicle.getAttrs());
93+
itemList.add(new LocalizableItem(Vehicle.getName(), Vehicle.getAttrs(), toMap(info), toMap(findOtherLang(file))));
8294
}//动物
8395
else if (path.contains("Animals")) {
84-
fillList(info, Animals.getName(), Animals.getAttrs());
96+
itemList.add(new LocalizableItem(Vehicle.getName(), Vehicle.getAttrs(), toMap(info), toMap(findOtherLang(file))));
8597
}
8698
}
8799
/* //用于NPC文件的判断——任务、NPC、对话、商店

src/io/Items/LocalizableItem.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package io.Items;
22

3-
import java.util.Map;
4-
import java.util.*;
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.List;
56

67
import static Language.LanguageManager.getI18nText;
78

89
public class LocalizableItem implements Comparable<LocalizableItem>, Comparator<LocalizableItem> {
910

10-
final HashMap<String, String> infoMap;
11+
protected final HashMap<String, String> infoMap;
12+
protected final HashMap<String, String> langMap;
1113
private final String type;
1214
private final int id;
13-
public String getLang(String key) {
14-
return infoMap.getOrDefault(key, "");
15-
}
1615

17-
public LocalizableItem(String type, List<String> keys, HashMap<String, String> dataMap) {
16+
17+
public LocalizableItem(String type, List<String> keys, HashMap<String, String> dataMap, HashMap<String, String> langMap) {
1818
this.type = type;
1919
dataMap.keySet().retainAll(keys);
20+
langMap.keySet().retainAll(keys);
2021
id = Integer.parseInt(dataMap.getOrDefault("ID", "0"));
2122
infoMap = dataMap;
23+
this.langMap = langMap;
24+
}
25+
26+
public String getOriginal(String key) {
27+
return infoMap.getOrDefault(key, "");
28+
}
29+
30+
public String getLang(String key) {
31+
return langMap.getOrDefault(key, "");
2232
}
2333

2434
public int getId() {
@@ -29,12 +39,8 @@ public String getType() {
2939
return type;
3040
}
3141

32-
public Map<String, String> getLang() {
33-
return Collections.unmodifiableMap(infoMap);
34-
}
35-
3642
public boolean setLang(String key, String value) {
37-
return infoMap.replace(key, value) != null;
43+
return langMap.replace(key, value) != null;
3844
}
3945

4046
@Override
@@ -49,7 +55,12 @@ public int compareTo(LocalizableItem o) {
4955

5056
@Override
5157
public String toString() {
52-
return id + " " + infoMap.getOrDefault("Name", getI18nText("result.namenoexist"));
58+
return id
59+
+ " "
60+
+ infoMap.getOrDefault("Name", getI18nText("result.namenoexist"))
61+
+ " 【"
62+
+ langMap.getOrDefault("Name", getI18nText("result.namenoexist"))
63+
+ "】";
5364
}
5465

5566
public boolean equals(Object obj) {

src/io/Items/Map.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import static Language.LanguageManager.getI18nText;
77

88
public class Map extends LocalizableItem {
9-
public Map(String type, List<String> keys, HashMap<String, String> dataMap) {
10-
super(type, keys, dataMap);
9+
public Map(String type, List<String> keys, HashMap<String, String> dataMap, HashMap<String, String> langMap) {
10+
super(type, keys, dataMap, langMap);
1111
}
1212

1313
@Override

0 commit comments

Comments
 (0)