From 655e1f8b072218d5774f57a929c8eeca3a9a9093 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 9 May 2019 14:42:20 +0200 Subject: [PATCH] Automatically add missing translation entries Entries will be appended to the bottom of the selected language file with English default values --- .../config/LanguageConfiguration.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/epiceric/shopchest/config/LanguageConfiguration.java b/src/main/java/de/epiceric/shopchest/config/LanguageConfiguration.java index 207c3fb..921edc2 100644 --- a/src/main/java/de/epiceric/shopchest/config/LanguageConfiguration.java +++ b/src/main/java/de/epiceric/shopchest/config/LanguageConfiguration.java @@ -16,6 +16,7 @@ public class LanguageConfiguration extends FileConfiguration { private ShopChest plugin; private boolean showMessages; + private File file; public LanguageConfiguration(ShopChest plugin, boolean showMessages) { this.plugin = plugin; @@ -41,13 +42,30 @@ public class LanguageConfiguration extends FileConfiguration { return values.get(key); } } + + values.put(path, def); + + if (file != null) { + // Append missing entry to loaded language file + try (FileWriter writer = new FileWriter(file, true)) { + writer.write(path + "=" + def + "\n"); + if (showMessages) + plugin.getLogger().info("Missing translation for \"" + path + "\" has been added as \"" + def + "\" to the selected language file."); + } catch (IOException e) { + plugin.debug("Failed to add language entry"); + plugin.debug(e); + if (showMessages) + plugin.getLogger().severe("Failed to add missing translation for \"" + path + "\" to the selected langauge file."); + } + } - if (showMessages) plugin.getLogger().info("Could not find translation for \"" + path + "\" in selected language file. Using default translation (" + def + ")"); return def; } @Override public void load(File file) throws IOException, InvalidConfigurationException { + this.file = file; + FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr);