Support providing the translation or the original message as a tooltip

This commit is contained in:
Johannes Frohnmeyer 2022-02-12 09:22:13 +01:00
parent 533fa19630
commit a5dee00b14
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 19 additions and 5 deletions

View File

@ -7,4 +7,5 @@ public class GoogleChatConfig implements JfConfig {
@Entry public static Boolean enabled = true;
@Entry public static String serverLanguage = "auto";
@Entry public static String clientLanguage = "en";
@Entry public static Boolean translationTooltip = false;
}

View File

@ -10,9 +10,7 @@ import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.MessageType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import net.minecraft.text.*;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
@ -21,6 +19,7 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.List;
import java.util.UUID;
@Mixin(GameMessageS2CPacket.class)
@ -51,9 +50,21 @@ public class GameMessageS2CPacketMixin {
return true;
});
try {
message = new LiteralText(TranslateService.translate(sb.toString(), Language.byId(GoogleChatConfig.serverLanguage), client));
LiteralText translatedText = new LiteralText(TranslateService.translate(sb.toString(), Language.byId(GoogleChatConfig.serverLanguage), client));
if (GoogleChatConfig.translationTooltip)
message = googlechat$concat(message.getWithStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Translated: ").append(translatedText)))));
else
message = translatedText.setStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Original: ").append(message))));
} catch (TranslateException e) {
GoogleChat.LOGGER.error("Could not translate received message", e);
}
}
private Text googlechat$concat(List<Text> texts) {
MutableText res = null;
for (Text text : texts) {
res = res == null ? (MutableText) text : res.append(text);
}
return res;
}
}

View File

@ -5,5 +5,7 @@
"google-chat.jfconfig.serverLanguage": "Server Language",
"google-chat.jfconfig.serverLanguage.tooltip": "The language of the server used in translations. \"auto\" will disable translating your own messages",
"google-chat.jfconfig.clientLanguage": "Client Language",
"google-chat.jfconfig.clientLanguage.tooltip": "Your own language used in translations. \"auto\" will disable translating messages by other server members"
"google-chat.jfconfig.clientLanguage.tooltip": "Your own language used in translations. \"auto\" will disable translating messages by other server members",
"google-chat.jfconfig.translationTooltip": "Translation Tooltip",
"google-chat.jfconfig.translationTooltip.tooltip": "Display translations as a tooltip (on hover) and keep the original message"
}