Fix custom language files.

- Now actually using custom language file when indicated in the config
file.
- Set default language file to read-only.
- Added warning message to not alter the default language file as it'll
be regenerated.
This commit is contained in:
Pim van der Loos 2019-04-28 17:31:08 +02:00
parent 45d5f90fef
commit ecd0340cd6
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
4 changed files with 64 additions and 36 deletions

View File

@ -92,9 +92,6 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Y u do dis? :( // Y u do dis? :(
myLogger(Level.INFO, "Stats disabled, not loading stats :(... Please consider enabling it! I am a simple man, seeing higher user numbers helps me stay motivated!"); myLogger(Level.INFO, "Stats disabled, not loading stats :(... Please consider enabling it! I am a simple man, seeing higher user numbers helps me stay motivated!");
locale = config.getString("languageFile");
// Load the files for the correct version of Minecraft. // Load the files for the correct version of Minecraft.
if (compatibleMCVer()) if (compatibleMCVer())
{ {
@ -139,6 +136,8 @@ public class ArmoredElytra extends JavaPlugin implements Listener
// Check if the plugin should go into uninstall mode. // Check if the plugin should go into uninstall mode.
uninstallMode = config.getBool("uninstallMode"); uninstallMode = config.getBool("uninstallMode");
locale = config.getString("languageFile");
} }
public Messages getMyMessages() public Messages getMyMessages()
@ -223,6 +222,10 @@ public class ArmoredElytra extends JavaPlugin implements Listener
public String getLocale() public String getLocale()
{ {
if (locale == null)
System.out.println("locale is null!");
else
System.out.println("Locale is " + locale);
return locale == null ? "en_US" : locale; return locale == null ? "en_US" : locale;
} }

View File

@ -12,7 +12,6 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import nl.pim16aap2.armoredElytra.ArmoredElytra; import nl.pim16aap2.armoredElytra.ArmoredElytra;
import nl.pim16aap2.armoredElytra.util.ConfigOption;
public class ConfigLoader public class ConfigLoader
{ {
@ -35,7 +34,7 @@ public class ConfigLoader
public ConfigLoader(ArmoredElytra plugin) public ConfigLoader(ArmoredElytra plugin)
{ {
this.plugin = plugin; this.plugin = plugin;
configOptionsList = new ArrayList<ConfigOption>(); configOptionsList = new ArrayList<>();
makeConfig(); makeConfig();
} }

View File

@ -15,22 +15,30 @@ public class Messages
{ {
private Map<String, String> messageMap = new HashMap<>(); private Map<String, String> messageMap = new HashMap<>();
private ArmoredElytra plugin; private ArmoredElytra plugin;
private String locale;
private File textFile; private File textFile;
public Messages(ArmoredElytra plugin) public Messages(ArmoredElytra plugin)
{ {
this.plugin = plugin; this.plugin = plugin;
locale = plugin.getLocale(); textFile = new File(plugin.getDataFolder(), plugin.getLocale() + ".txt");
textFile = new File(plugin.getDataFolder(), locale + ".txt");
readFile(); readFile();
} }
private void writeDefaultFile()
{
File defaultFile = new File(plugin.getDataFolder(), "en_US.txt");
if (!defaultFile.setWritable(true))
plugin.myLogger(Level.SEVERE, "Failed to make file \"" + defaultFile + "\" writable!");
// Load the default en_US from the resources.
plugin.saveResource("en_US.txt", true);
defaultFile.setWritable(false);
}
// Read locale file. // Read locale file.
private void readFile() private void readFile()
{ {
// Load the default en_US from the resources. writeDefaultFile();
plugin.saveResource("en_US.txt", true);
try (BufferedReader br = new BufferedReader(new FileReader(textFile))) try (BufferedReader br = new BufferedReader(new FileReader(textFile)))
{ {
@ -38,6 +46,9 @@ public class Messages
while ((sCurrentLine = br.readLine()) != null) while ((sCurrentLine = br.readLine()) != null)
{ {
// Ignore comments.
if (sCurrentLine.startsWith("#"))
continue;
String key, value; String key, value;
String[] parts = sCurrentLine.split("=", 2); String[] parts = sCurrentLine.split("=", 2);
key = parts[0]; key = parts[0];
@ -55,11 +66,11 @@ public class Messages
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
plugin.myLogger(Level.SEVERE, "Locale file " + locale + ".txt does not exist!"); plugin.myLogger(Level.SEVERE, "Locale file \"" + textFile + "\" does not exist!");
} }
catch (IOException e) catch (IOException e)
{ {
plugin.myLogger(Level.SEVERE, "Could not read locale file! (" + locale + ".txt)"); plugin.myLogger(Level.SEVERE, "Could not read locale file: \"" + textFile + "\"");
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -71,9 +82,18 @@ public class Messages
value = messageMap.get(key); value = messageMap.get(key);
if (value == null) if (value == null)
{ {
value = "ArmoredElytra: Translation not found! Contact server admin!"; value = "BigDoors: Translation not found! Contact server admin!";
plugin.myLogger(Level.WARNING, "Failed to get translation for key " + key); plugin.myLogger(Level.WARNING, "Failed to get translation for key " + key);
} }
return value; return value;
} }
public String getStringReverse(String value)
{
return messageMap.entrySet().stream()
.filter(e -> e.getValue().equals(value))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
}
} }

View File

@ -1,3 +1,9 @@
# This file contains all the (partial) sentences used in this plugin.
# If you want to modify anything, make sure to do so in a copy, as this file will be regenerated on startup!
# You can change which file will be used in the config.yml.
# The format is "key=value" (without quotation marks). You can modify the values, but not the keys.
# Order doesn't matter and you can use comments if you so desire.
# Please do note that white space does matter! (so spaces at the end of lines, for example).
TIER.Leather=&2Leather Armored Elytra TIER.Leather=&2Leather Armored Elytra
TIER.Gold=&EGolden Armored Elytra TIER.Gold=&EGolden Armored Elytra
TIER.Chain=&8Chain Armored Elytra TIER.Chain=&8Chain Armored Elytra