diff --git a/src/main/java/net/wurstclient/util/GoogleTranslate.java b/src/main/java/net/wurstclient/util/GoogleTranslate.java index 705ca85b2f..30f78a754e 100644 --- a/src/main/java/net/wurstclient/util/GoogleTranslate.java +++ b/src/main/java/net/wurstclient/util/GoogleTranslate.java @@ -23,12 +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.equalsIgnoreCase(translated)) + // 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; @@ -96,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; @@ -106,4 +111,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(); + } }