Skip to content
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 89cc64c

Browse files
committed
i18n: 添加本地化测试
1 parent 8bf732f commit 89cc64c

File tree

7 files changed

+107
-46
lines changed

7 files changed

+107
-46
lines changed

src/main/java/xyz/mrcraftteammc/grasslauncher/common/DefaultExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import xyz.mrcraftteammc.grasslauncher.extension.Extension;
5+
import xyz.mrcraftteammc.grasslauncher.extension.ExtensionManifest;
56
import xyz.mrcraftteammc.grasslauncher.extension.annotations.ExtensionInstance;
67
import xyz.mrcraftteammc.grasslauncher.extension.exception.ExtensionException;
78

@@ -10,7 +11,7 @@ public class DefaultExtension extends Extension {
1011
private final Logger logger = super.getLogger();
1112

1213
public DefaultExtension() {
13-
super("grasslauncher");
14+
super(ExtensionManifest.defaultManifest());
1415
}
1516

1617
@Override
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xyz.mrcraftteammc.grasslauncher.common;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public enum GrassLauncherVersion {
9+
VERSION_1_0_0("1.0.0");
10+
11+
private final String version;
12+
13+
public static GrassLauncherVersion getLatest() {
14+
return VERSION_1_0_0;
15+
}
16+
}

src/main/java/xyz/mrcraftteammc/grasslauncher/extension/Extension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@Getter
1111
@AllArgsConstructor
1212
public abstract class Extension {
13-
private final String id;
13+
private final ExtensionManifest manifest;
1414

1515
public abstract void onLoad() throws ExtensionException;
1616

@@ -21,7 +21,7 @@ public void onDisabled() throws ExtensionException {
2121
}
2222

2323
public Logger getLogger() {
24-
return LoggerFactory.getLogger(String.format("GrassLauncher Extension | %s", this.id));
24+
return LoggerFactory.getLogger(String.format("GrassLauncher Extension | %s", this.manifest.getId()));
2525
}
2626

2727
public GrassLauncher getLauncherInstance() {

src/main/java/xyz/mrcraftteammc/grasslauncher/extension/ExtensionLoader.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package xyz.mrcraftteammc.grasslauncher.extension;
22

3-
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
65
import xyz.mrcraftteammc.grasslauncher.common.CommonConstants;
@@ -24,64 +23,65 @@ public final class ExtensionLoader {
2423
private static boolean Initialized = false;
2524
private final Logger logger = LoggerFactory.getLogger("GrassLauncher Extension Loader");
2625
private final File file = new File(CommonConstants.EXTENSIONS_DIR);
27-
private final YAMLMapper mapper = new YAMLMapper();
28-
private List<Extension> extensionList = new ArrayList<>();
26+
private final List<Extension> extensionList = new ArrayList<>();
27+
private final List<ExtensionManifest> extensionManifestList = new ArrayList<>();
2928

3029
public ExtensionLoader() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
3130
if (!Initialized) {
3231
Initialized = true;
33-
this.extensionList = this.getExtensions();
34-
} else {
35-
logger.error("Cannot create `ExtensionLoader` instance again!");
36-
}
37-
}
3832

39-
public List<Extension> getExtensions() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
40-
if (!file.exists()) throw new IOException("The extension path does not exist.");
33+
if (!file.exists()) throw new IOException("The extension path does not exist.");
34+
if (!file.isDirectory()) throw new IOException("The extension path is not a directory.");
4135

42-
if (!file.isDirectory()) throw new IOException("The extension path is not a directory.");
36+
List<File> files = new ArrayList<>();
4337

44-
List<File> files = new ArrayList<>();
45-
List<Extension> extensions = new ArrayList<>();
38+
this.extensionList.add(new DefaultExtension());
39+
this.extensionList.add(new i18nExtension());
4640

47-
extensions.add(new DefaultExtension());
48-
extensions.add(new i18nExtension());
49-
50-
for (File f : Objects.requireNonNull(file.listFiles())) {
51-
if (f.getName().endsWith(".jar")) {
52-
files.add(f);
41+
for (File f : Objects.requireNonNull(file.listFiles())) {
42+
if (f.getName().endsWith(".jar")) {
43+
files.add(f);
44+
}
5345
}
54-
}
5546

56-
for (File f : files) {
57-
try (JarFile jar = new JarFile(f)) {
58-
Enumeration<JarEntry> entries = jar.entries();
47+
for (File f : files) {
48+
try (JarFile jar = new JarFile(f)) {
49+
Enumeration<JarEntry> entries = jar.entries();
5950

60-
while (entries.hasMoreElements()) {
61-
JarEntry entry = entries.nextElement();
51+
while (entries.hasMoreElements()) {
52+
JarEntry entry = entries.nextElement();
6253

63-
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
64-
continue;
65-
}
66-
String clz = entry.getName()
67-
.substring(0, entry.getName().length() - 6)
68-
.replace('/', '.');
69-
URLClassLoader loader = new URLClassLoader(new URL[]{f.toURI().toURL()},
70-
Thread.currentThread().getContextClassLoader());
71-
Class<?> clazz = loader.loadClass(clz);
72-
73-
if (clazz.getAnnotation(ExtensionInstance.class) != null) {
74-
Object o = clazz.newInstance();
75-
76-
if (o instanceof Extension) {
77-
extensions.add((Extension) o);
54+
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
55+
continue;
56+
}
57+
58+
String clz = entry.getName()
59+
.substring(0, entry.getName().length() - 6)
60+
.replace('/', '.');
61+
URLClassLoader loader = new URLClassLoader(new URL[]{f.toURI().toURL()},
62+
Thread.currentThread().getContextClassLoader());
63+
Class<?> clazz = loader.loadClass(clz);
64+
65+
if (clazz.getAnnotation(ExtensionInstance.class) != null) {
66+
Object o = clazz.newInstance();
67+
68+
if (o instanceof Extension) {
69+
extensionList.add((Extension) o);
70+
ExtensionManifest manifest = ((Extension) o).getManifest();
71+
72+
if (this.extensionManifestList.isEmpty()) {
73+
this.extensionManifestList.add(manifest);
74+
} else if (!this.extensionManifestList.contains(manifest)) {
75+
this.extensionManifestList.add(manifest);
76+
}
77+
}
7878
}
7979
}
8080
}
8181
}
82+
} else {
83+
logger.error("Cannot create `ExtensionLoader` instance again!");
8284
}
83-
84-
return extensions;
8585
}
8686

8787
public void loadExtensions() {
@@ -92,6 +92,8 @@ public void loadExtensions() {
9292
logger.info("Loading Extensions...");
9393

9494
this.extensionList.forEach(Extension::onLoad);
95+
96+
this.extensionManifestList.forEach(System.err::println);
9597
}
9698

9799
public void enableExtensions() {

src/main/java/xyz/mrcraftteammc/grasslauncher/i18n/i18nExtension.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package xyz.mrcraftteammc.grasslauncher.i18n;
22

3+
import okhttp3.OkHttpClient;
34
import xyz.mrcraftteammc.grasslauncher.extension.Extension;
5+
import xyz.mrcraftteammc.grasslauncher.extension.ExtensionManifest;
46
import xyz.mrcraftteammc.grasslauncher.extension.annotations.ExtensionInstance;
57
import xyz.mrcraftteammc.grasslauncher.extension.exception.ExtensionException;
68

9+
import java.util.Locale;
10+
711
@ExtensionInstance
812
public class i18nExtension extends Extension {
13+
private final OkHttpClient client = new OkHttpClient();
14+
private final String url = "https://github.com/MrCraftTeamMC/GrassLauncher.i18n";
15+
private final Locale locale = Locale.getDefault(); // Now support Russian, English. ZH_CN. ZH_TW
16+
917
public i18nExtension() {
10-
super("grasslauncher");
18+
super(ExtensionManifest.defaultManifest());
1119
}
1220

1321
@Override
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package xyz.mrcraftteammc.grasslauncher.i18n.util;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public enum ExtendedLocale {
9+
ZH_CN("zh", "cn"),
10+
ZH_TW("zh", "tw"),
11+
12+
EN_US("en", "us"),
13+
EN_UK("en", "uk"),
14+
15+
RU_RU("ru", "ru"),
16+
17+
FR_FR("fr", "fr"),
18+
19+
DE_DE("de", "de");
20+
21+
private final String language;
22+
private final String country;
23+
private final char split_char = '_';
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package xyz.mrcraftteammc.grasslauncher.i18n
2+
3+
import org.junit.jupiter.api.Test
4+
5+
class i18nTest {
6+
@Test
7+
void lang() {
8+
println "${Locale.getDefault().getLanguage()}_${Locale.getDefault().getCountry().toLowerCase(Locale.ROOT)}"
9+
}
10+
}

0 commit comments

Comments
 (0)