diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2024-08-26 02:46:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-26 02:46:10 +0200 |
commit | a658f0f272c25d50ce3c572c388e07895735bd48 (patch) | |
tree | b5bb0e3a8891e5cffe31d60ff3978a7653c99df8 /src/main/java/at/hannibal2/skyhanni/features/chat | |
parent | 290676ba3954d653bc1cbc0630d2cde90a3249f4 (diff) | |
download | skyhanni-a658f0f272c25d50ce3c572c388e07895735bd48.tar.gz skyhanni-a658f0f272c25d50ce3c572c388e07895735bd48.tar.bz2 skyhanni-a658f0f272c25d50ce3c572c388e07895735bd48.zip |
Improvement: Multiple Languages (#2403)
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/translation/TranslatableLanguage.kt | 45 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/chat/translation/Translator.kt (renamed from src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt) | 65 |
2 files changed, 104 insertions, 6 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/translation/TranslatableLanguage.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/translation/TranslatableLanguage.kt new file mode 100644 index 000000000..13a19a1dc --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/translation/TranslatableLanguage.kt @@ -0,0 +1,45 @@ +package at.hannibal2.skyhanni.features.chat.translation + +enum class TranslatableLanguage(private val englishName: String, private val nativeName: String, val languageCode: String) { + + // 1. First Language - The primary language of the application. + ENGLISH("English", "", "en"), + + // 2. Well Supported - Major languages commonly used in Europe and North America. + SPANISH("Spanish", "Español", "es"), // Major language in Spain and Latin America + GERMAN("German", "Deutsch", "de"), // Important language in Germany, Austria, and Switzerland + FRENCH("French", "Français", "fr"), // Significant language in France, Canada, and parts of Africa + DUTCH("Dutch", "Nederlands", "nl"), // Spoken in the Netherlands and Belgium + RUSSIAN("Russian", "Русский", "ru"), // Major language in Russia and other parts of Eastern Europe and Central Asia + POLISH("Polish", "Polski", "pl"), // Spoken primarily in Poland + ITALIAN("Italian", "Italiano", "it"), // Important language in Italy and parts of Switzerland + UKRAINIAN("Ukrainian", "Українська", "uk"), // Spoken in Ukraine + PORTUGUESE("Portuguese", "Português", "pt"), // Spoken in Portugal and Brazil + TURKISH("Turkish", "Türkçe", "tr"), // Significant in Turkey and Central Asia + SWEDISH("Swedish", "Svenska", "sv"), // Relevant in Northern Europe + + // 3. Global Languages - Widely spoken languages with significant global presence. + CHINESE("Chinese", "中文", "zh"), // Major language in China and other parts of East Asia + ARABIC("Arabic", "العربية", "ar"), // Significant language in the Middle East and North Africa + JAPANESE("Japanese", "日本語", "ja"), // Major language in Japan + HINDI("Hindi", "हिन्दी", "hi"), // Major language in India + BENGALI("Bengali", "বাংলা", "bn"), // Widely spoken in India and Bangladesh + KOREAN("Korean", "한국어", "ko"), // Important for East Asia + VIETNAMESE("Vietnamese", "Tiếng Việt", "vi"), // Major language in Vietnam + INDONESIAN("Indonesian", "Bahasa Indonesia", "id"), // Key language in Southeast Asia + THAI("Thai", "ภาษาไทย", "th"), // Important in Thailand + + // 4. Other Supported Languages + PERSIAN("Persian", "فارسی", "fa"), // Spoken in Iran and other parts of the Middle East + TAGALOG("Tagalog", "Tagalog", "tl"), // Major language in the Philippines + PUNJABI("Punjabi", "ਪੰਜਾਬੀ", "pa"), // Significant in India and Pakistan + + // 5. need better name + UNKNOWN("Unknown Language", "", ""), + ; + + // Limit to 20 characters so that the text is not too small in the config + private val displayName: String = if (nativeName.isBlank()) englishName else "$englishName/$nativeName".take(20) + + override fun toString(): String = displayName +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/translation/Translator.kt index 2d3f3aa85..389b57fb0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/chat/Translator.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/chat/translation/Translator.kt @@ -1,13 +1,17 @@ -package at.hannibal2.skyhanni.features.chat +package at.hannibal2.skyhanni.features.chat.translation import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.SkyHanniMod.Companion.coroutineScope +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.events.ConfigLoadEvent 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.onToggle import at.hannibal2.skyhanni.utils.ConditionalUtils.transformIf import at.hannibal2.skyhanni.utils.OSUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.StringUtils.getPlayerNameFromChatMessage import at.hannibal2.skyhanni.utils.StringUtils.removeColor import com.google.gson.JsonArray @@ -20,6 +24,7 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.net.URLDecoder import java.net.URLEncoder +import kotlin.time.Duration.Companion.milliseconds // TODO split into two classes: TranslatorCommand and GoogleTranslator. only communicates via getTranslationFromEnglish and getTranslationToEnglish @SkyHanniModule @@ -44,6 +49,41 @@ object Translator { editedComponent.setChatStyle(clickStyle) } + @SubscribeEvent + fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { + event.move(55, "chat.translator", "chat.translator.translateOnClick") + } + + var lastUserChange = SimpleTimeMark.farPast() + + @SubscribeEvent + fun onConfigReload(event: ConfigLoadEvent) { + config.languageCode.onToggle { + if (lastUserChange.passedSince() < 50.milliseconds) return@onToggle + lastUserChange = SimpleTimeMark.now() + + val text = config.languageCode.get() + if (text.isEmpty()) { + config.languageName.set(TranslatableLanguage.ENGLISH) + } else { + for (language in TranslatableLanguage.values()) { + if (language.languageCode.equals(text, ignoreCase = true)) { + config.languageName.set(language) + return@onToggle + } + } + config.languageName.set(TranslatableLanguage.UNKNOWN) + } + } + + config.languageName.onToggle { + if (lastUserChange.passedSince() < 50.milliseconds) return@onToggle + lastUserChange = SimpleTimeMark.now() + + config.languageCode.set(config.languageName.get().languageCode) + } + } + private fun createClickStyle(message: String, style: ChatStyle): ChatStyle { val text = messageContentRegex.find(message)!!.groupValues[1].removeColor() style.setChatClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/shtranslate $text")) @@ -51,7 +91,7 @@ object Translator { return style } - private val config get() = SkyHanniMod.feature.chat + private val config get() = SkyHanniMod.feature.chat.translator /* * Simplified version of the JSON response: @@ -130,9 +170,22 @@ object Translator { 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") + var lang = config.languageCode.get() + val translation = if (lang.isEmpty()) { + getTranslationToEnglish(message) + } else { + getTranslationFromEnglish(message, lang) + } + if (message == translation) { + ChatUtils.userError("Translation is the same as the original message!") + return@launch + } + + if (translation == "Unable to translate!") { + ChatUtils.userError("Unable to translate message :( (is it in English?)") + return@launch + } + ChatUtils.chat("Found translation: §f$translation") } } @@ -151,5 +204,5 @@ object Translator { } } - fun isEnabled() = config.translator + fun isEnabled() = config.translateOnClick } |