Skip to content

Commit

Permalink
Turn GoogleTranslate into an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Oct 31, 2023
1 parent 8b2d762 commit b4bc2a1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/main/java/net/wurstclient/hacks/ChatTranslatorHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"google translator", "GoogleTranslation", "google translation"})
public final class ChatTranslatorHack extends Hack implements ChatInputListener
{
private static final GoogleTranslate googleTranslate =
new GoogleTranslate();

private final EnumSetting<FromLanguage> langFrom = new EnumSetting<>(
"Translate from", FromLanguage.values(), FromLanguage.AUTO_DETECT);

Expand Down Expand Up @@ -79,7 +76,7 @@ private void translate(ChatInputEvent event)
|| incomingMsg.startsWith(translatorPrefix))
return;

String translated = googleTranslate.translate(incomingMsg,
String translated = GoogleTranslate.translate(incomingMsg,
langFrom.getSelected().value, langTo.getSelected().value);

if(translated == null)
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/wurstclient/util/GoogleTranslate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GoogleTranslate
public enum GoogleTranslate
{
public String translate(String text, String langFrom, String langTo)
;

public static String translate(String text, String langFrom, String langTo)
{
String html = getHTML(text, langFrom, langTo);
String translated = parseHTML(html);
Expand All @@ -31,7 +33,7 @@ public String translate(String text, String langFrom, String langTo)
return translated;
}

private String getHTML(String text, String langFrom, String langTo)
private static String getHTML(String text, String langFrom, String langTo)
{
URL url = createURL(text, langFrom, langTo);

Expand All @@ -57,7 +59,7 @@ private String getHTML(String text, String langFrom, String langTo)
}
}

private URL createURL(String text, String langFrom, String langTo)
private static URL createURL(String text, String langFrom, String langTo)
{
try
{
Expand All @@ -75,7 +77,7 @@ private URL createURL(String text, String langFrom, String langTo)
}
}

private URLConnection setupConnection(URL url) throws IOException
private static URLConnection setupConnection(URL url) throws IOException
{
URLConnection connection = url.openConnection();

Expand All @@ -87,7 +89,7 @@ private URLConnection setupConnection(URL url) throws IOException
}

@SuppressWarnings("deprecation")
private String parseHTML(String html)
private static String parseHTML(String html)
{
String regex = "class=\"result-container\">([^<]*)<\\/div>";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Expand Down

0 comments on commit b4bc2a1

Please sign in to comment.