Read localization files as UTF-8
- The localization files were read with whatever char encoding before which could cause issues in some cases. They are now read as UTF-8. Fixes #8
This commit is contained in:
parent
93a290cea6
commit
926d49dc0c
@ -5,12 +5,14 @@ import nl.pim16aap2.armoredElytra.ArmoredElytra;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -21,7 +23,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
public class Messages
|
public class Messages
|
||||||
{
|
{
|
||||||
private static final String DEFAULTFILENAME = "en_US.txt";
|
private static final String DEFAULT_FILENAME = "en_US.txt";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The map of all messages.
|
* The map of all messages.
|
||||||
@ -30,10 +32,10 @@ public class Messages
|
|||||||
* <p>
|
* <p>
|
||||||
* Value: The translated message.
|
* Value: The translated message.
|
||||||
*/
|
*/
|
||||||
private Map<Message, String> messageMap = new EnumMap<>(Message.class);
|
private final Map<Message, String> messageMap = new EnumMap<>(Message.class);
|
||||||
|
|
||||||
private final ArmoredElytra plugin;
|
private final ArmoredElytra plugin;
|
||||||
private File textFile;
|
private Path textFile;
|
||||||
|
|
||||||
private static final Pattern matchDots = Pattern.compile("\\.");
|
private static final Pattern matchDots = Pattern.compile("\\.");
|
||||||
private static final Pattern matchNewLines = Pattern.compile("\\\\n");
|
private static final Pattern matchNewLines = Pattern.compile("\\\\n");
|
||||||
@ -43,27 +45,29 @@ public class Messages
|
|||||||
{
|
{
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
writeDefaultFile();
|
writeDefaultFile();
|
||||||
final String fileName = plugin.getConfigLoader().languageFile();
|
|
||||||
// Only append .txt if the provided name doesn't already have it.
|
|
||||||
textFile = new File(plugin.getDataFolder(), fileName.endsWith(".txt") ? fileName : (fileName + ".txt"));
|
|
||||||
|
|
||||||
if (!textFile.exists())
|
String fileName = plugin.getConfigLoader().languageFile();
|
||||||
|
fileName = fileName.endsWith(".txt") ? fileName : (fileName + ".txt");
|
||||||
|
|
||||||
|
textFile = Path.of(plugin.getDataFolder().toURI()).resolve(fileName);
|
||||||
|
|
||||||
|
if (!Files.exists(textFile))
|
||||||
{
|
{
|
||||||
plugin.myLogger(Level.WARNING, "Failed to load language file: \"" + textFile +
|
plugin.myLogger(Level.WARNING, "Failed to load language file: \"" + textFile +
|
||||||
"\": File not found! Using default file (\"" + DEFAULTFILENAME + "\") instead!");
|
"\": File not found! Using default file (\"" + DEFAULT_FILENAME + "\") instead!");
|
||||||
textFile = new File(plugin.getDataFolder(), DEFAULTFILENAME);
|
textFile = Path.of(plugin.getDataFolder().toURI()).resolve(DEFAULT_FILENAME);
|
||||||
}
|
}
|
||||||
populateMessageMap();
|
populateMessageMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeDefaultFile()
|
private void writeDefaultFile()
|
||||||
{
|
{
|
||||||
File defaultFile = new File(plugin.getDataFolder(), DEFAULTFILENAME);
|
File defaultFile = new File(plugin.getDataFolder(), DEFAULT_FILENAME);
|
||||||
|
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
URL url = getClass().getClassLoader().getResource(DEFAULTFILENAME);
|
URL url = getClass().getClassLoader().getResource(DEFAULT_FILENAME);
|
||||||
if (url == null)
|
if (url == null)
|
||||||
plugin.myLogger(Level.SEVERE, "Failed to read resources file from the jar! " +
|
plugin.myLogger(Level.SEVERE, "Failed to read resources file from the jar! " +
|
||||||
"The default translation file cannot be generated! Please contact pim16aap2");
|
"The default translation file cannot be generated! Please contact pim16aap2");
|
||||||
@ -166,7 +170,7 @@ public class Messages
|
|||||||
*/
|
*/
|
||||||
private void populateMessageMap()
|
private void populateMessageMap()
|
||||||
{
|
{
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(textFile)))
|
try (BufferedReader br = Files.newBufferedReader(textFile, StandardCharsets.UTF_8))
|
||||||
{
|
{
|
||||||
processFile(br, this::addMessage);
|
processFile(br, this::addMessage);
|
||||||
}
|
}
|
||||||
@ -181,10 +185,9 @@ public class Messages
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final URL defaultFileUrl = Objects.requireNonNull(getClass().getClassLoader().getResource(DEFAULT_FILENAME));
|
||||||
try (BufferedReader br = new BufferedReader(
|
try (BufferedReader br = new BufferedReader(
|
||||||
new InputStreamReader(
|
new InputStreamReader(defaultFileUrl.openStream(), StandardCharsets.UTF_8)))
|
||||||
Objects.requireNonNull(getClass().getClassLoader().getResource(DEFAULTFILENAME)).openStream())))
|
|
||||||
{
|
{
|
||||||
processFile(br, this::addBackupMessage);
|
processFile(br, this::addBackupMessage);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user