Tweak translation logic to support carpet (also needs update in LibJF)
ci/woodpecker/push/jfmod Pipeline was successful
Details
ci/woodpecker/push/jfmod Pipeline was successful
Details
This commit is contained in:
parent
1e21ba6cc6
commit
6f2c5b8572
|
@ -1,15 +1,16 @@
|
||||||
package io.gitlab.jfronny.googlechat.mixin;
|
package io.gitlab.jfronny.googlechat.mixin;
|
||||||
|
|
||||||
import io.gitlab.jfronny.googlechat.*;
|
import io.gitlab.jfronny.googlechat.GoogleChat;
|
||||||
import io.gitlab.jfronny.libjf.LibJf;
|
import net.minecraft.client.gui.screen.ChatScreen;
|
||||||
import net.minecraft.client.gui.screen.*;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.*;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.*;
|
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||||
|
|
||||||
@Mixin(ChatScreen.class)
|
@Mixin(ChatScreen.class)
|
||||||
public class ChatScreenMixin {
|
public class ChatScreenMixin {
|
||||||
@ModifyVariable(method = "sendMessage(Ljava/lang/String;Z)Z", at = @At(value = "HEAD"), argsOnly = true, ordinal = 0)
|
@ModifyVariable(method = "sendMessage(Ljava/lang/String;Z)Z", at = @At(value = "HEAD"), argsOnly = true, ordinal = 0)
|
||||||
String googlechat$translateChatText(String chatText) {
|
String googlechat$translateChatText(String chatText) {
|
||||||
|
if (chatText.startsWith("/")) return chatText; // Bypass for client-side commands (Carpet, ...)
|
||||||
return GoogleChat.translateIfNeeded(chatText, GoogleChat.Direction.C2S, true);
|
return GoogleChat.translateIfNeeded(chatText, GoogleChat.Direction.C2S, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,37 +37,25 @@ public class GoogleChat implements ModInitializer {
|
||||||
public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) {
|
public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) {
|
||||||
if (shouldSkipOutright(direction)) return source;
|
if (shouldSkipOutright(direction)) return source;
|
||||||
String sourceString = toString(source);
|
String sourceString = toString(source);
|
||||||
if (respectRegex && failsRegex(sourceString, direction))
|
if (respectRegex && failsRegex(sourceString, direction)) return source;
|
||||||
return source;
|
|
||||||
if (GoogleChatConfig.desugar) {
|
|
||||||
MutableText translatedText = Text.literal(translateIfNeeded(sourceString, direction, true));
|
|
||||||
if (GoogleChatConfig.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translatedText));
|
|
||||||
if (GoogleChatConfig.translationTooltip)
|
|
||||||
return source.copy().setStyle(addHover(Style.EMPTY, Text.literal("Translated: ").append(translatedText)));
|
|
||||||
else
|
|
||||||
return translatedText.setStyle(addHover(Style.EMPTY, Text.literal("Original: ").append(source)));
|
|
||||||
}
|
|
||||||
MutableText translated;
|
MutableText translated;
|
||||||
if (source.getContent() instanceof TranslatableTextContent tx) {
|
if (GoogleChatConfig.desugar) {
|
||||||
Object[] args = tx.getArgs();
|
translated = Text.literal(translateIfNeeded(sourceString, direction, true));
|
||||||
args = Arrays.copyOf(args, args.length);
|
|
||||||
// We're not translating TranslatableText, but are translating arguments
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
|
||||||
args[i] = args[i] instanceof Text tx1 ? translateIfNeeded(tx1, direction, false)
|
|
||||||
: args[i] instanceof String tx1 ? translateIfNeeded(tx1, direction, false)
|
|
||||||
: args[i];
|
|
||||||
}
|
|
||||||
translated = Text.translatable(tx.getKey(), args);
|
|
||||||
} else if (source.getContent() instanceof LiteralTextContent tx) {
|
|
||||||
translated = Text.literal(translateIfNeeded(tx.string(), direction, false)).setStyle(source.getStyle());
|
|
||||||
} else {
|
} else {
|
||||||
//LOGGER.info("Unhandled text type: " + source.getClass() + " (" + source + ")");
|
translated = MutableText.of(translateIfNeeded(source.getContent(), direction, false))
|
||||||
translated = source.copy();
|
.setStyle(source.getStyle());
|
||||||
|
for (Text sibling : source.getSiblings()) {
|
||||||
|
translated.append(translateIfNeeded(sibling, direction, false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (GoogleChatConfig.translationTooltip)
|
if (GoogleChatConfig.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translated));
|
||||||
return source.copy().styled(style -> addHover(style, translated));
|
if (GoogleChatConfig.translationTooltip) {
|
||||||
else
|
return source.copy().styled(style -> addHover(style, Text.literal("Translated: ").append(translated)));
|
||||||
|
} else if (translated.getStyle().getHoverEvent() == null) {
|
||||||
|
return translated.styled(style -> addHover(style, Text.literal("Original: ").append(source)));
|
||||||
|
} else {
|
||||||
return translated;
|
return translated;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String toString(Text text) {
|
private static String toString(Text text) {
|
||||||
|
@ -79,14 +67,46 @@ public class GoogleChat implements ModInitializer {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static TextContent translateIfNeeded(TextContent source, Direction direction, boolean respectRegex) {
|
||||||
|
if (shouldSkipOutright(direction)) return source;
|
||||||
|
String sourceString = toString(source);
|
||||||
|
if (respectRegex && failsRegex(sourceString, direction)) return source;
|
||||||
|
//TODO This method (and the check for translatable args) should be converted to a switch pattern when available
|
||||||
|
if (source instanceof TranslatableTextContent tx) {
|
||||||
|
Object[] args = tx.getArgs();
|
||||||
|
args = Arrays.copyOf(args, args.length);
|
||||||
|
// We're not translating TranslatableText, but are translating arguments
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
if (args[i] instanceof Text tx1) args[i] = translateIfNeeded(tx1, direction, false);
|
||||||
|
else if (args[i] instanceof TextContent tx1) args[i] = translateIfNeeded(tx1, direction, false);
|
||||||
|
else if (args[i] instanceof String tx1) args[i] = translateIfNeeded(tx1, direction, false);
|
||||||
|
else args[i] = args[i];
|
||||||
|
}
|
||||||
|
return new TranslatableTextContent(tx.getKey(), args);
|
||||||
|
} else if (source instanceof LiteralTextContent tx) {
|
||||||
|
return new LiteralTextContent(translateIfNeeded(tx.string(), direction, false));
|
||||||
|
} else {
|
||||||
|
// LOGGER.info("Unhandled text type: " + source.getClass() + " (" + source + ")");
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toString(TextContent text) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
text.visit(asString -> {
|
||||||
|
sb.append(asString);
|
||||||
|
return Optional.empty();
|
||||||
|
});
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static Style addHover(Style style, Text hoverText) {
|
private static Style addHover(Style style, Text hoverText) {
|
||||||
return style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText));
|
return style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String translateIfNeeded(String source, Direction direction, boolean respectRegex) {
|
public static String translateIfNeeded(String source, Direction direction, boolean respectRegex) {
|
||||||
if (shouldSkipOutright(direction)) return source;
|
if (shouldSkipOutright(direction)) return source;
|
||||||
if (respectRegex && failsRegex(source, direction))
|
if (respectRegex && failsRegex(source, direction)) return source;
|
||||||
return source;
|
|
||||||
// Ignore generics since this is apparently not something java supports
|
// Ignore generics since this is apparently not something java supports
|
||||||
@SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE;
|
@SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE;
|
||||||
Language sourceLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.clientLanguage : GoogleChatConfig.serverLanguage);
|
Language sourceLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.clientLanguage : GoogleChatConfig.serverLanguage);
|
||||||
|
|
Loading…
Reference in New Issue