From bc67675d160787e9f1cdfcdcd7cde6123ced57a4 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Thu, 9 Apr 2020 01:30:27 +0100 Subject: [PATCH] Added translation of extra message parts --- .../translators/java/JavaChatTranslator.java | 13 ++++--------- .../geysermc/connector/utils/LocaleUtils.java | 6 ++++-- .../connector/utils/MessageUtils.java | 19 ++++++------------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java index 017e0178..ae58830c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java @@ -60,25 +60,20 @@ public class JavaChatTranslator extends PacketTranslator { break; } + String locale = session.getClientData().getLanguageCode(); + if (packet.getMessage() instanceof TranslationMessage) { textPacket.setType(TextPacket.Type.TRANSLATION); textPacket.setNeedsTranslation(true); - String locale = session.getClientData().getLanguageCode(); - List paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale); textPacket.setParameters(paramsTranslated); - textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale), paramsTranslated)); + textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale, false), paramsTranslated)); } else { textPacket.setNeedsTranslation(false); - // This make every message get translated which fixes alot of formatting issues - // but also causes players to be able to send translation strings as messages - // if thats all they send - // textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), session.getClientData().getLanguageCode())); - - textPacket.setMessage(MessageUtils.getBedrockMessage(packet.getMessage())); + textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale, false)); } session.getUpstream().sendPacket(textPacket); diff --git a/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java b/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java index c057d4f7..3c6ca19b 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java @@ -42,6 +42,8 @@ public class LocaleUtils { private static final Map ASSET_MAP = new HashMap<>(); + private static final String DEFAULT_LOCALE = (GeyserConnector.getInstance().getConfig().getDefaultLocale() != null ? GeyserConnector.getInstance().getConfig().getDefaultLocale() : "en_us"); + static { // Create the locales folder File localesFolder = new File("locales/"); @@ -49,7 +51,7 @@ public class LocaleUtils { // Download the latest asset list and cache it generateAssetCache(); - downloadAndLoadLocale(GeyserConnector.getInstance().getConfig().getDefaultLocale()); + downloadAndLoadLocale(DEFAULT_LOCALE); } private static void generateAssetCache() { @@ -169,7 +171,7 @@ public class LocaleUtils { public static String getLocaleString(String messageText, String locale) { Map localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase()); if (localeStrings == null) - localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(GeyserConnector.getInstance().getConfig().getDefaultLocale()); + localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(DEFAULT_LOCALE); return localeStrings.getOrDefault(messageText, messageText); } 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 6e78321c..76563dd6 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -71,11 +71,7 @@ public class MessageUtils { } else { String builder = getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor()); - if (locale != null) { - builder += getTranslatedBedrockMessage(message, locale); - }else { - builder += getBedrockMessage(message); - } + builder += getTranslatedBedrockMessage(message, locale, false); strings.add(builder); } } @@ -92,7 +88,7 @@ public class MessageUtils { + "%" + message.getTranslationKey(); } - public static String getTranslatedBedrockMessage(Message message, String locale) { + public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate) { JsonParser parser = new JsonParser(); if (isMessage(message.getText())) { JsonObject object = parser.parse(message.getText()).getAsJsonObject(); @@ -100,7 +96,7 @@ public class MessageUtils { } String messageText = message.getText(); - if (locale != null) { + if (locale != null && shouldTranslate) { messageText = LocaleUtils.getLocaleString(messageText, locale); } @@ -109,18 +105,15 @@ public class MessageUtils { builder.append(getFormat(msg.getStyle().getFormats())); builder.append(getColor(msg.getStyle().getColor())); if (!(msg.getText() == null)) { - builder.append(getTranslatedBedrockMessage(msg, locale)); + boolean isTranslationMessage = (msg instanceof TranslationMessage); + builder.append(getTranslatedBedrockMessage(msg, locale, isTranslationMessage)); } } return builder.toString(); } - public static String getTranslatedBedrockMessage(Message message) { - return getTranslatedBedrockMessage(message, null); - } - public static String getBedrockMessage(Message message) { - return getTranslatedBedrockMessage(message); + return getTranslatedBedrockMessage(message, null, false); } public static String insertParams(String message, List params) {