aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/chat
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-06-14 05:49:38 +1000
committerGitHub <noreply@github.com>2024-06-13 21:49:38 +0200
commit5bd6a4cd3807d128ba55c336946531a8838c51c7 (patch)
tree3b3b4589cb4e47541f8d12602682a37f44b18a80 /src/main/java/at/hannibal2/skyhanni/features/chat
parente9c12d363a45d7cc1581fd882f3701b5388f4a8e (diff)
downloadskyhanni-5bd6a4cd3807d128ba55c336946531a8838c51c7.tar.gz
skyhanni-5bd6a4cd3807d128ba55c336946531a8838c51c7.tar.bz2
skyhanni-5bd6a4cd3807d128ba55c336946531a8838c51c7.zip
Backend: Apply annotations to everything (#1983)
* add plugin Co-authored-by: ThatGravyBoat <thatgravyboat@gmail.com> * data and apis done * other stuff done * fix merge * Backend: Classes -> Objects with annotation * Backend: Apply annotations to everything * fix test * fix merge * fix merge * use annotation * use annotation * fix version number * fix space * bring back fix * work on companion again now that they are all done * oop * fixed merge conflicts --------- Co-authored-by: ThatGravyBoat <thatgravyboat@gmail.com> Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/chat')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt185
1 files changed, 92 insertions, 93 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt
index 683fe1857..2d3f3aa85 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt
@@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.features.chat
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.SkyHanniMod.Companion.coroutineScope
import at.hannibal2.skyhanni.events.LorenzChatEvent
+import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.APIUtil
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ConditionalUtils.transformIf
@@ -21,7 +22,8 @@ import java.net.URLDecoder
import java.net.URLEncoder
// TODO split into two classes: TranslatorCommand and GoogleTranslator. only communicates via getTranslationFromEnglish and getTranslationToEnglish
-class Translator {
+@SkyHanniModule
+object Translator {
private val messageContentRegex = Regex(".*: (.*)")
@@ -49,108 +51,105 @@ class Translator {
return style
}
- companion object {
-
- private val config get() = SkyHanniMod.feature.chat
-
- /*
- * Simplified version of the JSON response:
- * [
- * [
- * [
- * 'translated sentence one with a space after the punctuation. '
- * 'original sentence one without a space after the punctuation.'
- * ],
- * [
- * 'translated sentence two without punctuation bc it's last'
- * 'original sentence two without punctuation'
- * ]
- * ],
- * null,
- * '"target language as a two-letter code following ISO 639-1"',
- * ]
- */
-
- private fun getJSONResponse(urlString: String) =
- APIUtil.getJSONResponseAsElement(urlString, false, "Google Translate API")
-
- private fun getTranslationToEnglish(message: String): String {
- val url =
- "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=" +
- URLEncoder.encode(message, "UTF-8")
-
- var messageToSend = ""
- val layer1 = getJSONResponse(url).asJsonArray
- if (layer1.size() <= 2) return "Error!"
-
- val language = layer1[2].toString()
- if (language == "\"en\"") return "Unable to translate!"
- if (language.length != 4) return "Error!"
-
- val layer2 = try {
- layer1[0] as JsonArray
- } catch (_: Exception) {
- return "Error!"
- }
+ private val config get() = SkyHanniMod.feature.chat
+
+ /*
+ * Simplified version of the JSON response:
+ * [
+ * [
+ * [
+ * 'translated sentence one with a space after the punctuation. '
+ * 'original sentence one without a space after the punctuation.'
+ * ],
+ * [
+ * 'translated sentence two without punctuation bc it's last'
+ * 'original sentence two without punctuation'
+ * ]
+ * ],
+ * null,
+ * '"target language as a two-letter code following ISO 639-1"',
+ * ]
+ */
+
+ private fun getJSONResponse(urlString: String) =
+ APIUtil.getJSONResponseAsElement(urlString, false, "Google Translate API")
+
+ private fun getTranslationToEnglish(message: String): String {
+ val url =
+ "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=" +
+ URLEncoder.encode(message, "UTF-8")
+
+ var messageToSend = ""
+ val layer1 = getJSONResponse(url).asJsonArray
+ if (layer1.size() <= 2) return "Error!"
+
+ val language = layer1[2].toString()
+ if (language == "\"en\"") return "Unable to translate!"
+ if (language.length != 4) return "Error!"
+
+ val layer2 = try {
+ layer1[0] as JsonArray
+ } catch (_: Exception) {
+ return "Error!"
+ }
- for (layer3 in layer2) {
- val arrayLayer3 = layer3 as? JsonArray ?: continue
- val sentence = arrayLayer3[0].toString()
+ for (layer3 in layer2) {
+ val arrayLayer3 = layer3 as? JsonArray ?: continue
+ val sentence = arrayLayer3[0].toString()
+ val sentenceWithoutQuotes = sentence.substring(1, sentence.length - 1)
+ messageToSend = "$messageToSend$sentenceWithoutQuotes"
+ }
+ messageToSend = "$messageToSend §7(Language: $language)"
+
+ return URLDecoder.decode(messageToSend, "UTF-8").replace("\\", "")
+ }
+
+ private fun getTranslationFromEnglish(message: String, lang: String): String {
+ val url =
+ "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=$lang&dt=t&q=" +
+ URLEncoder.encode(message, "UTF-8")
+
+ val layer1 = getJSONResponse(url).asJsonArray
+ if (layer1.size() < 1) return "Error!"
+ val layer2 = layer1[0] as? JsonArray
+
+ val firstSentence = (layer2?.get(0) as? JsonArray)?.get(0).toString()
+ var messageToSend = firstSentence.substring(0, firstSentence.length - 1)
+ if (layer2 != null) {
+ for (sentenceIndex in 1..<layer2.size()) {
+ val sentence = (layer2.get(sentenceIndex) as JsonArray).get(0).toString()
val sentenceWithoutQuotes = sentence.substring(1, sentence.length - 1)
messageToSend = "$messageToSend$sentenceWithoutQuotes"
}
- messageToSend = "$messageToSend §7(Language: $language)"
+ } // The first translated sentence only has 1 extra char at the end, but sentences after it need 1 at the front and 1 at the end removed in the substring
+ messageToSend = messageToSend.substring(1, messageToSend.length)
+ return URLDecoder.decode(messageToSend, "UTF-8").replace("\\", "")
+ }
- return URLDecoder.decode(messageToSend, "UTF-8").replace("\\", "")
- }
+ fun toEnglish(args: Array<String>) {
+ val message = args.joinToString(" ").removeColor()
- private fun getTranslationFromEnglish(message: String, lang: String): String {
- val url =
- "https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=$lang&dt=t&q=" +
- URLEncoder.encode(message, "UTF-8")
-
- val layer1 = getJSONResponse(url).asJsonArray
- if (layer1.size() < 1) return "Error!"
- val layer2 = layer1[0] as? JsonArray
-
- val firstSentence = (layer2?.get(0) as? JsonArray)?.get(0).toString()
- var messageToSend = firstSentence.substring(0, firstSentence.length - 1)
- if (layer2 != null) {
- for (sentenceIndex in 1..<layer2.size()) {
- val sentence = (layer2.get(sentenceIndex) as JsonArray).get(0).toString()
- val sentenceWithoutQuotes = sentence.substring(1, sentence.length - 1)
- messageToSend = "$messageToSend$sentenceWithoutQuotes"
- }
- } // The first translated sentence only has 1 extra char at the end, but sentences after it need 1 at the front and 1 at the end removed in the substring
- messageToSend = messageToSend.substring(1, messageToSend.length)
- return URLDecoder.decode(messageToSend, "UTF-8").replace("\\", "")
+ coroutineScope.launch {
+ val translation = getTranslationToEnglish(message)
+ if (translation == "Unable to translate!") ChatUtils.userError("Unable to translate message :( (is it in English?)")
+ else ChatUtils.chat("Found translation: §f$translation")
}
+ }
- fun toEnglish(args: Array<String>) {
- val message = args.joinToString(" ").removeColor()
-
- coroutineScope.launch {
- val translation = getTranslationToEnglish(message)
- if (translation == "Unable to translate!") ChatUtils.userError("Unable to translate message :( (is it in English?)")
- else ChatUtils.chat("Found translation: §f$translation")
- }
+ fun fromEnglish(args: Array<String>) {
+ if (args.size < 2 || args[0].length != 2) { // args[0] is the language code
+ ChatUtils.userError("Usage: /shcopytranslation <two letter language code (at the end of a translation)> <message>")
+ return
}
+ val language = args[0]
+ val message = args.drop(1).joinToString(" ")
- fun fromEnglish(args: Array<String>) {
- if (args.size < 2 || args[0].length != 2) { // args[0] is the language code
- ChatUtils.userError("Usage: /shcopytranslation <two letter language code (at the end of a translation)> <message>")
- return
- }
- val language = args[0]
- val message = args.drop(1).joinToString(" ")
-
- coroutineScope.launch {
- val translation = getTranslationFromEnglish(message, language)
- ChatUtils.chat("Copied translation to clipboard: §f$translation")
- OSUtils.copyToClipboard(translation)
- }
+ coroutineScope.launch {
+ val translation = getTranslationFromEnglish(message, language)
+ ChatUtils.chat("Copied translation to clipboard: §f$translation")
+ OSUtils.copyToClipboard(translation)
}
-
- fun isEnabled() = config.translator
}
+
+ fun isEnabled() = config.translator
}