From d5828861dea20034daab5c82ce4561900be029c8 Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Wed, 29 Jan 2020 04:31:09 -0800 Subject: [PATCH] Always use UTF-8 for loading translations (cherry picked from commit 624b3a449b5746cee84557992b9b217ac135cf50) --- .../util/translation/TranslationManager.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java index 47c05e552..516dde7e1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/translation/TranslationManager.java @@ -33,7 +33,9 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.Reader; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import java.util.HashSet; import java.util.Locale; @@ -85,15 +87,17 @@ public class TranslationManager { .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); } - private Map parseTranslationFile(InputStream inputStream) { - return filterTranslations(gson.fromJson(new InputStreamReader(inputStream), STRING_MAP_TYPE)); + private Map parseTranslationFile(InputStream inputStream) throws IOException { + try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { + return filterTranslations(gson.fromJson(reader, STRING_MAP_TYPE)); + } } private Optional> loadTranslationFile(String filename) { Map baseTranslations; - try { - baseTranslations = parseTranslationFile(ResourceLoader.getResourceRoot("lang/" + filename).openStream()); + try (InputStream stream = ResourceLoader.getResourceRoot("lang/" + filename).openStream()) { + baseTranslations = parseTranslationFile(stream); } catch (IOException e) { // Seem to be missing base. If the user has provided a file use that. baseTranslations = new ConcurrentHashMap<>(); @@ -101,8 +105,8 @@ public class TranslationManager { File localFile = worldEdit.getWorkingDirectoryFile("lang/" + filename); if (localFile.exists()) { - try { - baseTranslations.putAll(parseTranslationFile(new FileInputStream(localFile))); + try (InputStream stream = new FileInputStream(localFile)) { + baseTranslations.putAll(parseTranslationFile(stream)); } catch (IOException e) { // Failed to parse custom language file. Worth printing. e.printStackTrace();