From 330427f5a56e01ecc494db4e042edad17f1eec40 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 7 May 2022 18:12:53 -0400 Subject: [PATCH] Simplify the language fallback system --- src/main/java/emu/grasscutter/Config.java | 2 +- .../java/emu/grasscutter/Grasscutter.java | 5 ++- .../java/emu/grasscutter/utils/Language.java | 44 +++++-------------- .../java/emu/grasscutter/utils/Utils.java | 3 +- 4 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/main/java/emu/grasscutter/Config.java b/src/main/java/emu/grasscutter/Config.java index 1111ba920..3982fc46b 100644 --- a/src/main/java/emu/grasscutter/Config.java +++ b/src/main/java/emu/grasscutter/Config.java @@ -23,7 +23,7 @@ public final class Config { public GameServerOptions GameServer = new GameServerOptions(); public DispatchServerOptions DispatchServer = new DispatchServerOptions(); public Locale LocaleLanguage = Locale.getDefault(); - public Locale DefaultLanguage = Locale.US; + public Locale DefaultLanguage = Locale.ENGLISH; public Boolean OpenStamina = true; public GameServerOptions getGameServerOptions() { diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index 51bc3fcc5..c593f5f13 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -149,9 +149,10 @@ public final class Grasscutter { public static void loadLanguage() { var locale = config.LocaleLanguage; - String languageTag = locale.toLanguageTag(); + var languageTag = locale.toLanguageTag(); + if (languageTag.equals("und")) { - Grasscutter.getLogger().error("Illegal locale language, using en-US instead."); + Grasscutter.getLogger().error("Illegal locale language, using 'en-US' instead."); language = Language.getLanguage("en-US"); } else { language = Language.getLanguage(languageTag); diff --git a/src/main/java/emu/grasscutter/utils/Language.java b/src/main/java/emu/grasscutter/utils/Language.java index 340d35dc2..0af77adc1 100644 --- a/src/main/java/emu/grasscutter/utils/Language.java +++ b/src/main/java/emu/grasscutter/utils/Language.java @@ -19,7 +19,7 @@ public final class Language { * @return A language instance. */ public static Language getLanguage(String langCode) { - return new Language(langCode + ".json"); + return new Language(langCode + ".json", Grasscutter.getConfig().DefaultLanguage.toLanguageTag()); } /** @@ -30,6 +30,7 @@ public final class Language { */ public static String translate(String key, Object... args) { String translated = Grasscutter.getLanguage().get(key); + try { return translated.formatted(args); } catch (Exception exception) { @@ -38,48 +39,25 @@ public final class Language { } } - /** - * creates a language instance. - * @param fileName The name of the language file. - */ - private Language(String fileName) { - @Nullable JsonObject languageData = null; - - languageData = loadLanguage(fileName); - - if (languageData == null) { - Grasscutter.getLogger().info("Now switch to default language"); - languageData = loadDefaultLanguage(); - } - - assert languageData != null : "languageData is null"; - this.languageData = languageData; - } - - /** - * Load default language file and creates a language instance. - * @return language data - */ - private JsonObject loadDefaultLanguage() { - var fileName = Grasscutter.getConfig().DefaultLanguage.toLanguageTag() + ".json"; - return loadLanguage(fileName); - } - /** * Reads a file and creates a language instance. * @param fileName The name of the language file. - * @return language data */ - private JsonObject loadLanguage(String fileName) { + private Language(String fileName, String fallback) { @Nullable JsonObject languageData = null; - + try { InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName); + if(file == null) { + file = Grasscutter.class.getResourceAsStream("/languages/" + fallback); + } + languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class); } catch (Exception exception) { - Grasscutter.getLogger().warn("Failed to load language file: " + fileName); + Grasscutter.getLogger().warn("Failed to load language file: " + fileName, exception); } - return languageData; + + this.languageData = languageData; } /** diff --git a/src/main/java/emu/grasscutter/utils/Utils.java b/src/main/java/emu/grasscutter/utils/Utils.java index 259fc8ad5..6d11822f0 100644 --- a/src/main/java/emu/grasscutter/utils/Utils.java +++ b/src/main/java/emu/grasscutter/utils/Utils.java @@ -1,6 +1,7 @@ package emu.grasscutter.utils; import java.io.*; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.time.*; @@ -254,7 +255,7 @@ public final class Utils { */ public static String readFromInputStream(InputStream stream) { StringBuilder stringBuilder = new StringBuilder(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"UTF-8"))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } stream.close();