Adds support for custom strings.yml files

This commit is contained in:
Kristian Knarvik 2022-01-19 21:22:37 +01:00
parent 23c023a027
commit 1c64e8813d
2 changed files with 42 additions and 5 deletions

View File

@ -133,7 +133,6 @@ public final class PermissionSigns extends JavaPlugin {
public void onEnable() { public void onEnable() {
PluginDescriptionFile pluginDescriptionFile = this.getDescription(); PluginDescriptionFile pluginDescriptionFile = this.getDescription();
pluginVersion = pluginDescriptionFile.getVersion(); 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 //TODO: Display a notice in the console if a new version is available
FileConfiguration config = this.getConfig(); FileConfiguration config = this.getConfig();

View File

@ -5,7 +5,10 @@ import net.knarcraft.permissionsigns.utility.FileHelper;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -23,7 +26,10 @@ public final class Translator {
*/ */
public static void loadLanguages(String selectedLanguage) { public static void loadLanguages(String selectedLanguage) {
backupTranslatedMessages = loadTranslatedMessages("en"); backupTranslatedMessages = loadTranslatedMessages("en");
translatedMessages = loadTranslatedMessages(selectedLanguage); translatedMessages = loadCustomTranslatedMessages(selectedLanguage);
if (translatedMessages == null) {
translatedMessages = loadTranslatedMessages(selectedLanguage);
}
} }
/** /**
@ -52,14 +58,46 @@ public final class Translator {
* @return <p>A mapping of all strings for the given language</p> * @return <p>A mapping of all strings for the given language</p>
*/ */
public static Map<TranslatableMessage, String> loadTranslatedMessages(String language) { public static Map<TranslatableMessage, String> loadTranslatedMessages(String language) {
Map<TranslatableMessage, String> translatedMessages = new HashMap<>();
BufferedReader reader;
try { try {
reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml");
return loadTranslatableMessages(language, reader);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages");
return null; return null;
} }
}
/**
* Tries to load translated messages from a custom strings.yml file
*
* @param language <p>The selected language</p>
* @return <p>The loaded translated strings, or null if no custom language file exists</p>
*/
public static Map<TranslatableMessage, String> 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 <p>The selected language</p>
* @param reader <p>The buffered reader to read from</p>
* @return <p>The loaded translated strings</p>
*/
private static Map<TranslatableMessage, String> loadTranslatableMessages(String language, BufferedReader reader) {
Map<TranslatableMessage, String> translatedMessages = new HashMap<>();
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader);
for (TranslatableMessage message : TranslatableMessage.values()) { for (TranslatableMessage message : TranslatableMessage.values()) {