1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-28 05:54:16 +02:00

Add option for custom translation file

This commit is contained in:
Shy
2026-04-17 15:24:23 +02:00
parent a947a303e1
commit f3ea35fd93
3 changed files with 69 additions and 4 deletions
@@ -74,7 +74,11 @@ public final class SModerationPaper extends JavaPlugin {
LOGGER.info("Folia: {}", SchedulerUtil.isFolia ? "yes" : "no"); LOGGER.info("Folia: {}", SchedulerUtil.isFolia ? "yes" : "no");
PLUGIN = this; PLUGIN = this;
LOGGER.info("Loading translations"); LOGGER.info("Loading translations");
if (config().getBoolean("custom-messages", false)) {
translatorManager.loadCustomMessages(getDataPath().resolve("messages.json"));
} else {
translatorManager.load(); translatorManager.load();
}
updateConfig(); updateConfig();
this.punishmentManager = new PunishmentManager(getDataPath().resolve("punishments.v2")); this.punishmentManager = new PunishmentManager(getDataPath().resolve("punishments.v2"));
@@ -1,17 +1,20 @@
package de.shiewk.smoderation.paper.translation; package de.shiewk.smoderation.paper.translation;
import com.google.gson.FormattingStyle;
import com.google.gson.JsonIOException; import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;
import de.shiewk.smoderation.paper.SModerationPaper; import de.shiewk.smoderation.paper.SModerationPaper;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.translation.MiniMessageTranslationStore; import net.kyori.adventure.text.minimessage.translation.MiniMessageTranslationStore;
import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.GlobalTranslator;
import java.io.IOException; import java.io.*;
import java.io.InputStream; import java.nio.file.Files;
import java.io.InputStreamReader; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@@ -45,4 +48,54 @@ public class TranslatorManager {
} }
GlobalTranslator.translator().addSource(translationStore); GlobalTranslator.translator().addSource(translationStore);
} }
public void loadCustomMessages(Path customPath) {
try {
if (Files.notExists(customPath)) {
Files.createDirectories(customPath.getParent());
Files.write(customPath, "{}".getBytes(), StandardOpenOption.CREATE);
}
Map<String, String> predefinedMap;
try (InputStream stream = SModerationPaper.class.getClassLoader().getResourceAsStream(resourcePath + "en_us.json")) {
if (stream == null) {
SModerationPaper.LOGGER.warn("English (US) predefined translations not found or not accessible");
predefinedMap = Map.of();
} else {
predefinedMap = SModerationPaper.gson.fromJson(new InputStreamReader(stream), new TypeToken<>(){});
}
}
Map<String, String> translationMap;
try (InputStream stream = Files.newInputStream(customPath)) {
translationMap = SModerationPaper.gson.fromJson(new InputStreamReader(stream), new TypeToken<>(){});
}
boolean updated = false;
for (Map.Entry<String, String> entry : predefinedMap.entrySet()) {
if (!translationMap.containsKey(entry.getKey())) {
translationMap.put(entry.getKey(), entry.getValue());
updated = true;
}
}
if (updated) {
SModerationPaper.LOGGER.warn("Updating {} custom translations", translationMap.size());
try (OutputStream stream = Files.newOutputStream(customPath, StandardOpenOption.TRUNCATE_EXISTING);
OutputStreamWriter writer = new OutputStreamWriter(stream);
JsonWriter jsonWriter = new JsonWriter(writer)) {
jsonWriter.setFormattingStyle(FormattingStyle.PRETTY);
SModerationPaper.gson.toJson(translationMap, translationMap.getClass(), jsonWriter);
}
SModerationPaper.LOGGER.info("Update successful");
}
translationStore.registerAll(Locale.forLanguageTag("en-US"), translationMap);
SModerationPaper.LOGGER.info("Registered {} custom translations", translationMap.size());
} catch (IOException e) {
SModerationPaper.LOGGER.warn("Failed to load custom translations", e);
}
GlobalTranslator.translator().addSource(translationStore);
}
} }
+8
View File
@@ -31,6 +31,14 @@ colors:
secondary: '#fc9e07' secondary: '#fc9e07'
detail: '#ababab' detail: '#ababab'
# Allows you to customize translation files.
# - At 'false', the plugin's predefined localization is used
# and available for multiple languages:
# https://github.com/Shiewk/SModeration/tree/main/src/main/resources/smoderation/translations
# - At 'true', a JSON localization file will be created in the plugin directory.
# Only the contained strings will be used and localization will be disabled.
custom-messages: false
# A list of commands which will be captured and broadcast # A list of commands which will be captured and broadcast
# to players which have SocialSpy enabled (/socialspy). # to players which have SocialSpy enabled (/socialspy).
socialspy-commands: socialspy-commands: