From 8c9d1fe09f7d7eff90b12619df140177e980650c Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Sun, 15 May 2022 14:23:52 -0400 Subject: [PATCH] Allow language file overrides By placing a locale file in `languages/ll_CC.properties`, any strings in that file will take priority over Geyser's own. --- .gitignore | 3 +- .../geysermc/geyser/text/GeyserLocale.java | 43 ++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 401002e1d..f1baa3abb 100644 --- a/.gitignore +++ b/.gitignore @@ -244,4 +244,5 @@ locales/ /cache/ /packs/ /dump.json -/saved-refresh-tokens.json \ No newline at end of file +/saved-refresh-tokens.json +/languages/ \ No newline at end of file diff --git a/core/src/main/java/org/geysermc/geyser/text/GeyserLocale.java b/core/src/main/java/org/geysermc/geyser/text/GeyserLocale.java index da6ea4dc0..86e015c0f 100644 --- a/core/src/main/java/org/geysermc/geyser/text/GeyserLocale.java +++ b/core/src/main/java/org/geysermc/geyser/text/GeyserLocale.java @@ -28,10 +28,10 @@ package org.geysermc.geyser.text; import org.geysermc.geyser.GeyserBootstrap; import org.geysermc.geyser.GeyserImpl; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; @@ -116,12 +116,22 @@ public class GeyserLocale { return locale; } + Properties localeProp = new Properties(); + + File localLanguage; + Path localFolder = bootstrap.getConfigFolder().resolve("languages"); + if (Files.exists(localFolder)) { + localLanguage = localFolder.resolve(locale + ".properties").toFile(); + } else { + localLanguage = null; + } + boolean validLocalLanguage = localLanguage != null && localLanguage.exists(); + InputStream localeStream = bootstrap.getResourceOrNull("languages/texts/" + locale + ".properties"); // Load the locale if (localeStream != null) { try { - Properties localeProp = new Properties(); try (InputStreamReader reader = new InputStreamReader(localeStream, StandardCharsets.UTF_8)) { localeProp.load(reader); } catch (Exception e) { @@ -130,18 +140,37 @@ public class GeyserLocale { // Insert the locale into the mappings LOCALE_MAPPINGS.put(locale, localeProp); - return locale; } finally { try { localeStream.close(); } catch (IOException ignored) {} } } else { - if (GeyserImpl.getInstance() != null) { + if (GeyserImpl.getInstance() != null && !validLocalLanguage) { + // Don't warn on missing locales if a local file has been found GeyserImpl.getInstance().getLogger().warning("Missing locale: " + locale); } - return null; } + + // Load any language overrides that exist after, to override any strings that we just added + // By loading both, we ensure that if a language string doesn't exist in the custom properties folder, + // it's loaded from our jar + if (validLocalLanguage) { + try (InputStream stream = new FileInputStream(localLanguage)) { + localeProp.load(stream); + } catch (IOException e) { + String message = "Unable to load custom language override!"; + if (GeyserImpl.getInstance() != null) { + GeyserImpl.getInstance().getLogger().error(message, e); + } else { + System.err.println(message); + e.printStackTrace(); + } + } + + LOCALE_MAPPINGS.putIfAbsent(locale, localeProp); + } + return localeProp.isEmpty() ? null : locale; } /**