From b5ce83bbe23bec035ff595b65a9383801aed93ef Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 5 Apr 2020 02:37:39 +0100 Subject: [PATCH] Missed changes --- .../connector/utils/MessageUtils.java | 57 ++++++++++++++----- .../org/geysermc/connector/utils/Toolbox.java | 2 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java index 978f15c0..0872d076 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -36,27 +36,43 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; +import org.geysermc.connector.GeyserConnector; import java.io.InputStream; import java.util.*; public class MessageUtils { - private static final HashMap LANG_MAPPINGS = new HashMap<>(); + private static final HashMap> LOCALE_MAPPINGS = new HashMap<>(); static { /* Load the language mappings */ - InputStream stream = Toolbox.getResource("mappings/lang.json"); - JsonNode lang; + InputStream languagesStream = Toolbox.getResource("mappings/locales.json"); + JsonNode locales; try { - lang = Toolbox.JSON_MAPPER.readTree(stream); + locales = Toolbox.JSON_MAPPER.readTree(languagesStream); } catch (Exception e) { - throw new AssertionError("Unable to load Java lang mappings", e); + throw new AssertionError("Unable to load Java locale list", e); } - Iterator> langIterator = lang.fields(); - while (langIterator.hasNext()) { - Map.Entry entry = langIterator.next(); - LANG_MAPPINGS.put(entry.getKey(), entry.getValue().asText()); + for (JsonNode localeNode : locales.get("locales")) { + String currentLocale = localeNode.asText(); + + InputStream stream = Toolbox.getResource("mappings/lang/" + currentLocale + ".json"); + JsonNode lang; + try { + lang = Toolbox.JSON_MAPPER.readTree(stream); + } catch (Exception e) { + throw new AssertionError("Unable to load Java lang map for " + currentLocale, e); + } + + Iterator> langIterator = lang.fields(); + HashMap langMap = new HashMap<>(); + while (langIterator.hasNext()) { + Map.Entry entry = langIterator.next(); + langMap.put(entry.getKey(), entry.getValue().asText()); + } + + LOCALE_MAPPINGS.put(currentLocale.toLowerCase(), langMap); } } @@ -94,7 +110,7 @@ public class MessageUtils { + "%" + message.getTranslationKey(); } - public static String getBedrockMessageWithTranslate(Message message, boolean convertLangStrings) { + public static String getBedrockMessageWithTranslate(Message message, String locale) { JsonParser parser = new JsonParser(); if (isMessage(message.getText())) { JsonObject object = parser.parse(message.getText()).getAsJsonObject(); @@ -102,8 +118,8 @@ public class MessageUtils { } String messageText = message.getText(); - if (convertLangStrings) { - messageText = getLangConversion(messageText); + if (locale != null) { + messageText = getLocaleString(messageText, locale); } StringBuilder builder = new StringBuilder(messageText); @@ -117,12 +133,23 @@ public class MessageUtils { return builder.toString(); } - private static String getLangConversion(String messageText) { - return LANG_MAPPINGS.getOrDefault(messageText, messageText); + public static String getBedrockMessageWithTranslate(Message message) { + return getBedrockMessageWithTranslate(message, null); + } + + private static String getLocaleString(String messageText, String locale) { + HashMap localeStrings = LOCALE_MAPPINGS.get(locale.toLowerCase()); + if (localeStrings == null) + localeStrings = LOCALE_MAPPINGS.get("en_us"); + + String newLocaleString = localeStrings.getOrDefault(messageText, messageText); + + GeyserConnector.getInstance().getLogger().info("Converting '" + messageText + "' -> '" + newLocaleString + "'"); + return newLocaleString; } public static String getBedrockMessage(Message message) { - return getBedrockMessageWithTranslate(message, false); + return getBedrockMessageWithTranslate(message); } private static String getColor(ChatColor color) { diff --git a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java index 45802196..b48d5b54 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java @@ -40,7 +40,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.network.translators.item.ItemEntry; -import java.io.InputStream; +import java.io.*; import java.util.*; public class Toolbox {