From 5d8ea002a5fb99366c5c952c3e1647390bb6d6d4 Mon Sep 17 00:00:00 2001 From: pcm1k Date: Fri, 8 Oct 2021 17:22:55 -0400 Subject: [PATCH 1/4] improve recognition of untranslated text --- src/main/java/net/wurstclient/util/GoogleTranslate.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/wurstclient/util/GoogleTranslate.java b/src/main/java/net/wurstclient/util/GoogleTranslate.java index 7c960b917f..1a35969bc6 100644 --- a/src/main/java/net/wurstclient/util/GoogleTranslate.java +++ b/src/main/java/net/wurstclient/util/GoogleTranslate.java @@ -27,7 +27,8 @@ public String translate(String text, String langFrom, String langTo) String html = getHTML(text, langFrom, langTo); String translated = parseHTML(html); - if(text.equalsIgnoreCase(translated)) + if(text.replaceAll("\\s", "").equalsIgnoreCase( + translated.replaceAll("\\s", ""))) return null; return translated; From 6b9480a1b1d5ab93381519afaf809064d822c237 Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Thu, 18 Jul 2024 20:23:16 +0200 Subject: [PATCH 2/4] Run spotlessApply --- src/main/java/net/wurstclient/util/GoogleTranslate.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/wurstclient/util/GoogleTranslate.java b/src/main/java/net/wurstclient/util/GoogleTranslate.java index 62db887de7..2114b163b6 100644 --- a/src/main/java/net/wurstclient/util/GoogleTranslate.java +++ b/src/main/java/net/wurstclient/util/GoogleTranslate.java @@ -28,8 +28,8 @@ public static String translate(String text, String langFrom, String langTo) String html = getHTML(text, langFrom, langTo); String translated = parseHTML(html); - if(text.replaceAll("\\s", "").equalsIgnoreCase( - translated.replaceAll("\\s", ""))) + if(text.replaceAll("\\s", "") + .equalsIgnoreCase(translated.replaceAll("\\s", ""))) return null; return translated; From c904990a84ba7b8dc125120fff5d5f9b4530caad Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Thu, 18 Jul 2024 20:33:23 +0200 Subject: [PATCH 3/4] Refactor whitespace check for better performance --- .../java/net/wurstclient/util/GoogleTranslate.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/wurstclient/util/GoogleTranslate.java b/src/main/java/net/wurstclient/util/GoogleTranslate.java index 2114b163b6..0520704d03 100644 --- a/src/main/java/net/wurstclient/util/GoogleTranslate.java +++ b/src/main/java/net/wurstclient/util/GoogleTranslate.java @@ -23,13 +23,16 @@ public enum GoogleTranslate { ; + private static final Pattern ALL_WHITESPACE = Pattern.compile("\\s+"); + public static String translate(String text, String langFrom, String langTo) { String html = getHTML(text, langFrom, langTo); String translated = parseHTML(html); - if(text.replaceAll("\\s", "") - .equalsIgnoreCase(translated.replaceAll("\\s", ""))) + // Detect if Google translate returned the original text, maybe with + // some whitespace or capitalization changes, and return null if so + if(simplify(text).equals(simplify(translated))) return null; return translated; @@ -107,4 +110,9 @@ private static String parseHTML(String html) // which isn't bundled with Minecraft return org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(match); } + + private static String simplify(String text) + { + return ALL_WHITESPACE.matcher(text).replaceAll("").toLowerCase(); + } } From c1d9bda0611bdd121c8335a1b7b4814eefaee87a Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Thu, 18 Jul 2024 21:07:26 +0200 Subject: [PATCH 4/4] Fix the unrelated issue that CodeRabbit found --- src/main/java/net/wurstclient/util/GoogleTranslate.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/wurstclient/util/GoogleTranslate.java b/src/main/java/net/wurstclient/util/GoogleTranslate.java index 0520704d03..30f78a754e 100644 --- a/src/main/java/net/wurstclient/util/GoogleTranslate.java +++ b/src/main/java/net/wurstclient/util/GoogleTranslate.java @@ -100,9 +100,10 @@ private static String parseHTML(String html) Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); Matcher matcher = pattern.matcher(html); - matcher.find(); - String match = matcher.group(1); + if(!matcher.find()) + return null; + String match = matcher.group(1); if(match == null || match.isEmpty()) return null;