Skip to content

Commit

Permalink
Allow language file overrides
Browse files Browse the repository at this point in the history
By placing a locale file in `languages/ll_CC.properties`, any strings in that file will take priority over Geyser's own.
  • Loading branch information
Camotoy committed May 15, 2022
1 parent b885e22 commit 8c9d1fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,5 @@ locales/
/cache/
/packs/
/dump.json
/saved-refresh-tokens.json
/saved-refresh-tokens.json
/languages/
43 changes: 36 additions & 7 deletions core/src/main/java/org/geysermc/geyser/text/GeyserLocale.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
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;
Expand Down Expand Up @@ -116,12 +116,22 @@ private static String loadGeyserLocale(String locale, GeyserBootstrap bootstrap)
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) {
Expand All @@ -130,18 +140,37 @@ private static String loadGeyserLocale(String locale, GeyserBootstrap bootstrap)

// 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;
}

/**
Expand Down

0 comments on commit 8c9d1fe

Please sign in to comment.