Added translation of extra message parts

This commit is contained in:
rtm516 2020-04-09 01:30:27 +01:00
parent 70a71b2ee8
commit bc67675d16
3 changed files with 14 additions and 24 deletions

View file

@ -60,25 +60,20 @@ public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
break; break;
} }
String locale = session.getClientData().getLanguageCode();
if (packet.getMessage() instanceof TranslationMessage) { if (packet.getMessage() instanceof TranslationMessage) {
textPacket.setType(TextPacket.Type.TRANSLATION); textPacket.setType(TextPacket.Type.TRANSLATION);
textPacket.setNeedsTranslation(true); textPacket.setNeedsTranslation(true);
String locale = session.getClientData().getLanguageCode();
List<String> paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale); List<String> paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale);
textPacket.setParameters(paramsTranslated); 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 { } else {
textPacket.setNeedsTranslation(false); textPacket.setNeedsTranslation(false);
// This make every message get translated which fixes alot of formatting issues textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale, false));
// 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()));
} }
session.getUpstream().sendPacket(textPacket); session.getUpstream().sendPacket(textPacket);

View file

@ -42,6 +42,8 @@ public class LocaleUtils {
private static final Map<String, Asset> ASSET_MAP = new HashMap<>(); private static final Map<String, Asset> ASSET_MAP = new HashMap<>();
private static final String DEFAULT_LOCALE = (GeyserConnector.getInstance().getConfig().getDefaultLocale() != null ? GeyserConnector.getInstance().getConfig().getDefaultLocale() : "en_us");
static { static {
// Create the locales folder // Create the locales folder
File localesFolder = new File("locales/"); File localesFolder = new File("locales/");
@ -49,7 +51,7 @@ public class LocaleUtils {
// Download the latest asset list and cache it // Download the latest asset list and cache it
generateAssetCache(); generateAssetCache();
downloadAndLoadLocale(GeyserConnector.getInstance().getConfig().getDefaultLocale()); downloadAndLoadLocale(DEFAULT_LOCALE);
} }
private static void generateAssetCache() { private static void generateAssetCache() {
@ -169,7 +171,7 @@ public class LocaleUtils {
public static String getLocaleString(String messageText, String locale) { public static String getLocaleString(String messageText, String locale) {
Map<String, String> localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase()); Map<String, String> localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase());
if (localeStrings == null) if (localeStrings == null)
localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(GeyserConnector.getInstance().getConfig().getDefaultLocale()); localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(DEFAULT_LOCALE);
return localeStrings.getOrDefault(messageText, messageText); return localeStrings.getOrDefault(messageText, messageText);
} }

View file

@ -71,11 +71,7 @@ public class MessageUtils {
} else { } else {
String builder = getFormat(message.getStyle().getFormats()) + String builder = getFormat(message.getStyle().getFormats()) +
getColor(message.getStyle().getColor()); getColor(message.getStyle().getColor());
if (locale != null) { builder += getTranslatedBedrockMessage(message, locale, false);
builder += getTranslatedBedrockMessage(message, locale);
}else {
builder += getBedrockMessage(message);
}
strings.add(builder); strings.add(builder);
} }
} }
@ -92,7 +88,7 @@ public class MessageUtils {
+ "%" + message.getTranslationKey(); + "%" + message.getTranslationKey();
} }
public static String getTranslatedBedrockMessage(Message message, String locale) { public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate) {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
if (isMessage(message.getText())) { if (isMessage(message.getText())) {
JsonObject object = parser.parse(message.getText()).getAsJsonObject(); JsonObject object = parser.parse(message.getText()).getAsJsonObject();
@ -100,7 +96,7 @@ public class MessageUtils {
} }
String messageText = message.getText(); String messageText = message.getText();
if (locale != null) { if (locale != null && shouldTranslate) {
messageText = LocaleUtils.getLocaleString(messageText, locale); messageText = LocaleUtils.getLocaleString(messageText, locale);
} }
@ -109,18 +105,15 @@ public class MessageUtils {
builder.append(getFormat(msg.getStyle().getFormats())); builder.append(getFormat(msg.getStyle().getFormats()));
builder.append(getColor(msg.getStyle().getColor())); builder.append(getColor(msg.getStyle().getColor()));
if (!(msg.getText() == null)) { if (!(msg.getText() == null)) {
builder.append(getTranslatedBedrockMessage(msg, locale)); boolean isTranslationMessage = (msg instanceof TranslationMessage);
builder.append(getTranslatedBedrockMessage(msg, locale, isTranslationMessage));
} }
} }
return builder.toString(); return builder.toString();
} }
public static String getTranslatedBedrockMessage(Message message) {
return getTranslatedBedrockMessage(message, null);
}
public static String getBedrockMessage(Message message) { public static String getBedrockMessage(Message message) {
return getTranslatedBedrockMessage(message); return getTranslatedBedrockMessage(message, null, false);
} }
public static String insertParams(String message, List<String> params) { public static String insertParams(String message, List<String> params) {