From 1c64e8813d394cab9e031d61130ccc420e2d1039 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 19 Jan 2022 21:22:37 +0100 Subject: [PATCH] Adds support for custom strings.yml files --- .../permissionsigns/PermissionSigns.java | 1 - .../formatting/Translator.java | 46 +++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java index 6da75b1..cceea99 100644 --- a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java +++ b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java @@ -133,7 +133,6 @@ public final class PermissionSigns extends JavaPlugin { public void onEnable() { PluginDescriptionFile pluginDescriptionFile = this.getDescription(); pluginVersion = pluginDescriptionFile.getVersion(); - //TODO: Allow for custom language files. Perhaps just look for strings.yml in the folder //TODO: Display a notice in the console if a new version is available FileConfiguration config = this.getConfig(); diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java index 6da4a9d..c6ab28f 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java @@ -5,7 +5,10 @@ import net.knarcraft.permissionsigns.utility.FileHelper; import org.bukkit.configuration.file.YamlConfiguration; import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; @@ -23,7 +26,10 @@ public final class Translator { */ public static void loadLanguages(String selectedLanguage) { backupTranslatedMessages = loadTranslatedMessages("en"); - translatedMessages = loadTranslatedMessages(selectedLanguage); + translatedMessages = loadCustomTranslatedMessages(selectedLanguage); + if (translatedMessages == null) { + translatedMessages = loadTranslatedMessages(selectedLanguage); + } } /** @@ -52,14 +58,46 @@ public final class Translator { * @return

A mapping of all strings for the given language

*/ public static Map loadTranslatedMessages(String language) { - Map translatedMessages = new HashMap<>(); - BufferedReader reader; try { - reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); + BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); + return loadTranslatableMessages(language, reader); } catch (FileNotFoundException e) { PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); return null; } + } + + /** + * Tries to load translated messages from a custom strings.yml file + * + * @param language

The selected language

+ * @return

The loaded translated strings, or null if no custom language file exists

+ */ + public static Map loadCustomTranslatedMessages(String language) { + File strings = new File(PermissionSigns.getInstance().getDataFolder(), "strings.yml"); + if (!strings.exists()) { + PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Strings file not found"); + return null; + } + + try { + PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Loading custom strings..."); + return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings)))); + } catch (FileNotFoundException e) { + PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Unable to load custom messages"); + return null; + } + } + + /** + * Loads translatable messages from the given reader + * + * @param language

The selected language

+ * @param reader

The buffered reader to read from

+ * @return

The loaded translated strings

+ */ + private static Map loadTranslatableMessages(String language, BufferedReader reader) { + Map translatedMessages = new HashMap<>(); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader); for (TranslatableMessage message : TranslatableMessage.values()) {