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

Commit ccf953f

Browse files
committed
i18n: 使用 Crowdin
1 parent 89cc64c commit ccf953f

File tree

12 files changed

+132
-78
lines changed

12 files changed

+132
-78
lines changed

build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,32 @@ test {
181181
}
182182
}
183183

184+
tasks.register("DownloadTranslate") {
185+
def langLibs = { Project p ->
186+
p.buildDir.toPath().resolve()
187+
}
188+
189+
delete fileTree(langLibs(rootProject)) {
190+
include '*'
191+
}
192+
193+
download {
194+
src ""
195+
dest project.buildDir
196+
}
197+
198+
exec {
199+
commandLine ""
200+
}
201+
202+
copy {
203+
from ""
204+
into ""
205+
}
206+
}
207+
208+
//jar.dependsOn "DownloadTranslate"
209+
184210
jar {
185211
manifest {
186212
attributes "Manifest-Version": "1.0"

src/main/java/xyz/mrcraftteammc/grasslauncher/common/core/Builder.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/main/java/xyz/mrcraftteammc/grasslauncher/common/core/CallBackEvent.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package xyz.mrcraftteammc.grasslauncher.common.core;
22

3-
import javax.annotation.Nullable;
4-
53
@FunctionalInterface
64
public interface Factory<T> {
7-
T work(@Nullable Object... objects);
5+
T build();
86
}

src/main/java/xyz/mrcraftteammc/grasslauncher/common/core/OptionalSupplier.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package xyz.mrcraftteammc.grasslauncher.common.core;
22

33
@FunctionalInterface
4-
public interface Property<T> {
5-
T get();
4+
public interface Property<T extends Comparable<T>, C> {
5+
T get(C type);
66
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
5+
import lombok.ToString;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78
import xyz.mrcraftteammc.grasslauncher.common.GrassLauncher;
89
import xyz.mrcraftteammc.grasslauncher.extension.exception.ExtensionException;
910

11+
@ToString
1012
@Getter
1113
@AllArgsConstructor
1214
public abstract class Extension {
@@ -20,6 +22,9 @@ public void onEnabled() throws ExtensionException {
2022
public void onDisabled() throws ExtensionException {
2123
}
2224

25+
public void onReload() throws ExtensionException {
26+
}
27+
2328
public Logger getLogger() {
2429
return LoggerFactory.getLogger(String.format("GrassLauncher Extension | %s", this.manifest.getId()));
2530
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ public ExtensionLoader() throws IOException, ClassNotFoundException, Instantiati
6969
extensionList.add((Extension) o);
7070
ExtensionManifest manifest = ((Extension) o).getManifest();
7171

72-
if (this.extensionManifestList.isEmpty()) {
73-
this.extensionManifestList.add(manifest);
74-
} else if (!this.extensionManifestList.contains(manifest)) {
75-
this.extensionManifestList.add(manifest);
76-
}
72+
this.extensionManifestList.add(manifest);
7773
}
7874
}
7975
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package xyz.mrcraftteammc.grasslauncher.i18n;
2+
3+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
4+
import lombok.Getter;
5+
import lombok.ToString;
6+
import org.jetbrains.annotations.ApiStatus;
7+
import xyz.mrcraftteammc.grasslauncher.common.util.config.TypeReferences;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.util.Locale;
12+
import java.util.Map;
13+
import java.util.ResourceBundle;
14+
15+
@ToString
16+
@Getter
17+
public abstract class Text {
18+
private final Locale locale = Locale.getDefault();
19+
20+
public abstract String getText(String id, Object[] args, String defaultText) throws IOException;
21+
22+
public String getText(String id, Object[] args) throws IOException {
23+
return this.getText(id, args, "");
24+
}
25+
26+
public String getText(String id) throws IOException {
27+
return this.getText(id, null);
28+
}
29+
30+
public static Text literal() {
31+
return new LiteralText();
32+
}
33+
34+
public static Text translatable() {
35+
return new TranslatableText();
36+
}
37+
38+
@ApiStatus.Experimental
39+
public static Text immutable() {
40+
return null;
41+
}
42+
43+
private static final class LiteralText extends Text {
44+
@Override
45+
public String getText(String id, Object[] args, String defaultText) {
46+
if (id == null || id.isEmpty()) {
47+
return defaultText;
48+
}
49+
50+
return id;
51+
}
52+
}
53+
54+
private static final class TranslatableText extends Text {
55+
@Override
56+
public String getText(String id, Object[] args, String defaultText) throws IOException {
57+
if (id == null || id.isEmpty()) {
58+
return defaultText;
59+
}
60+
61+
ResourceBundle bundle = ResourceBundle.getBundle("i18n.lang", Locale.getDefault());
62+
String text = bundle.getString(id);
63+
64+
if (args != null) {
65+
for (Object o : args) {
66+
text = text.replaceFirst("%?", o.toString());
67+
}
68+
}
69+
70+
return text;
71+
}
72+
}
73+
74+
@ApiStatus.Experimental
75+
private static abstract class ImmutableText extends Text {
76+
}
77+
78+
@FunctionalInterface
79+
public interface TextSupplier {
80+
String get(String id);
81+
}
82+
83+
@FunctionalInterface
84+
public interface TextModifiable {
85+
String toText();
86+
}
87+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
import xyz.mrcraftteammc.grasslauncher.extension.annotations.ExtensionInstance;
77
import xyz.mrcraftteammc.grasslauncher.extension.exception.ExtensionException;
88

9-
import java.util.Locale;
10-
119
@ExtensionInstance
1210
public class i18nExtension extends Extension {
1311
private final OkHttpClient client = new OkHttpClient();
1412
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
13+
private final Text text = Text.translatable();
1614

1715
public i18nExtension() {
1816
super(ExtensionManifest.defaultManifest());
1917
}
2018

2119
@Override
2220
public void onLoad() throws ExtensionException {
23-
this.getLogger().info("Hello i18nExtension!");
21+
this.getLogger().info("Loading Language files...");
22+
23+
try {
24+
this.getLogger().info(this.text.getText("helloworld"));
25+
} catch (Exception e) {
26+
this.getLogger().warn("Fail to get Language. Cause: ", e);
27+
}
2428
}
2529

2630
@Override

0 commit comments

Comments
 (0)