Skip to content

Commit

Permalink
实现服务端侧读取语言文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Sep 17, 2024
1 parent 396679c commit 3f03c68
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 7 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,19 @@
* Write a class implement `dev.anvilcraft.rg.api.RGAdditional`

```java
@Mod("your_mod_id")
public class YourMod {
public YourMod(IEventBus modEventBus, @NotNull ModContainer modContainer) {
modContainer.registerExtensionPoint(RGAdditional.class, new YourAdditional());
}
}

public class YourAdditional implements RGAdditional {
@Override
public void loadRules(@NotNull RGRuleManager manager) {
manager.register(YourRules.class);
TranslationUtil.loadLanguage(YourMod.class, "your_mod_id", "zh_cn");
TranslationUtil.loadLanguage(YourMod.class, "your_mod_id", "en_us");
}
}
```
Expand All @@ -132,13 +141,15 @@ public class YourAdditional implements RGAdditional {
```java
@Mod('your_mod_id')
public class YourMod implements RGAdditional {
public YourMod(IEventBus modEventBus, ModContainer modContainer) {
// ...
public YourMod(IEventBus modEventBus, @NotNull ModContainer modContainer) {
modContainer.registerExtensionPoint(RGAdditional.class, this);
}

@Override
public void loadRules(@NotNull RGRuleManager manager) {
manager.register(YourRules.class);
TranslationUtil.loadLanguage(YourMod.class, "your_mod_id", "zh_cn");
TranslationUtil.loadLanguage(YourMod.class, "your_mod_id", "en_us");
}
}
```
4 changes: 4 additions & 0 deletions src/main/java/dev/anvilcraft/rg/RollingGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.logging.LogUtils;
import dev.anvilcraft.rg.api.RGAdditional;
import dev.anvilcraft.rg.api.RGRuleManager;
import dev.anvilcraft.rg.util.TranslationUtil;
import net.minecraft.server.MinecraftServer;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
Expand All @@ -26,11 +27,14 @@ public class RollingGate implements RGAdditional {
public RollingGate(@NotNull IEventBus modEventBus, ModContainer modContainer) {
NeoForge.EVENT_BUS.addListener(this::reInitRules);
modEventBus.addListener(this::loadRGRules);
modContainer.registerExtensionPoint(RGAdditional.class, this);
}

@Override
public void loadRules(@NotNull RGRuleManager manager) {
manager.register(RollingGateRules.class);
TranslationUtil.loadLanguage(RollingGate.class, "rolling_gate", "zh_cn");
TranslationUtil.loadLanguage(RollingGate.class, "rolling_gate", "en_us");
}

@SubscribeEvent
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/anvilcraft/rg/RollingGateCategories.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dev.anvilcraft.rg;

public class RollingGateCategories {
public static final String DISABLED = "DISABLED";
public static final String DISABLED = "disabled";
}
4 changes: 2 additions & 2 deletions src/main/java/dev/anvilcraft/rg/RollingGateRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import dev.anvilcraft.rg.api.Rule;

public class RollingGateRules {
@Rule(allowed = {"true", "false"}, categories = {RollingGateCategories.DISABLED})
public static final boolean antiCheatDisabled = false;
@Rule(allowed = {"zh_cn", "en_us"}, categories = {RollingGateCategories.DISABLED})
public static final String language = "zh_cn";
}
3 changes: 1 addition & 2 deletions src/main/java/dev/anvilcraft/rg/api/RGRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public void setFieldValue(JsonElement primitive) {
case "float", "java.lang.Float" -> primitive.getAsFloat();
case "double", "java.lang.Double" -> primitive.getAsDouble();
case "java.lang.String" -> primitive.getAsString();
default ->
throw new RGRuleException("Field %s has unsupported type %s", this.field.getName(), this.field.getType().getTypeName());
default -> throw new RGRuleException("Field %s has unsupported type %s", this.field.getName(), this.field.getType().getTypeName());
};
this.setFieldValue((T) value);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/dev/anvilcraft/rg/api/RGRuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ public void setNamespace(String namespace) {
}
return ruleList;
}

/**
* 获取分组翻译键
*
* @return 返回格式化的分组翻译键字符串
*/
public @NotNull String getDescriptionCategoryKey(String category) {
// 使用String.format方法构建描述翻译键,包含命名空间和序列化值
return "rolling_gate.category.%s".formatted(category);
}
}
56 changes: 56 additions & 0 deletions src/main/java/dev/anvilcraft/rg/util/TranslationUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.anvilcraft.rg.util;

import com.google.gson.Gson;
import dev.anvilcraft.rg.RollingGate;
import dev.anvilcraft.rg.RollingGateRules;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class TranslationUtil {
public static final Gson GSON = new Gson();
public static final Map<String, Map<String, String>> LANGUAGES = new HashMap<>();

public static @NotNull String trans(String key) {
return LANGUAGES.getOrDefault(RollingGateRules.language, new HashMap<>()).getOrDefault(key, key);
}

public static @NotNull Component trans(String key, Object... args) {
return Component.translatableWithFallback(
key,
LANGUAGES.getOrDefault(RollingGateRules.language, new HashMap<>())
.getOrDefault(key, key)
.formatted(args),
args
);
}

public static void addLanguage(String language, Map<String, String> translations) {
Map<String, String> languages = LANGUAGES.getOrDefault(language, new HashMap<>());
languages.putAll(translations);
LANGUAGES.putIfAbsent(language, languages);
}

@SuppressWarnings("unchecked")
public static void loadLanguage(Class<?> clazz, String namespace, String language) {
try (
InputStream stream = clazz.getResourceAsStream("/assets/%s/lang/%s.json".formatted(namespace, language));
) {
if (stream == null) {
RollingGate.LOGGER.error("Can't find language {}/{}.", namespace, language);
return;
}
try (InputStreamReader reader = new InputStreamReader(stream)) {
TranslationUtil.addLanguage("zh_cn", (Map<String, String>) TranslationUtil.GSON.fromJson(reader, Map.class));
RollingGate.LOGGER.info("Loaded {} language file.", language);
}
} catch (IOException e) {
RollingGate.LOGGER.error("Failed to load %s language file.".formatted(language), e);
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/rolling_gate/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rolling_gate.rolling_gate.rule.language": "Language",
"rolling_gate.rolling_gate.rule.language.desc": "Set the default language for RollingGate",

"rolling_gate.category.disabled": "DISABLED"
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/rolling_gate/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rolling_gate.rolling_gate.rule.language": "语言",
"rolling_gate.rolling_gate.rule.language.desc": "设置卷帘门的默认语言",

"rolling_gate.category.disabled": "禁用类"
}

0 comments on commit 3f03c68

Please sign in to comment.