Skip to content

Commit

Permalink
Improvement: Multiple Languages (hannibal002#2403)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <[email protected]>
  • Loading branch information
hannibal002 and hannibal002 authored Aug 26, 2024
1 parent 290676b commit a658f0f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.google.gson.JsonPrimitive
object ConfigUpdaterMigrator {

val logger = LorenzLogger("ConfigMigration")
const val CONFIG_VERSION = 54
const val CONFIG_VERSION = 55
fun JsonElement.at(chain: List<String>, init: Boolean): JsonElement? {
if (chain.isEmpty()) return this
if (this !is JsonObject) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import at.hannibal2.skyhanni.data.TitleManager
import at.hannibal2.skyhanni.data.bazaar.HypixelBazaarFetcher
import at.hannibal2.skyhanni.features.bingo.card.BingoCardDisplay
import at.hannibal2.skyhanni.features.bingo.card.nextstephelper.BingoNextStepHelper
import at.hannibal2.skyhanni.features.chat.Translator
import at.hannibal2.skyhanni.features.chat.translation.Translator
import at.hannibal2.skyhanni.features.combat.endernodetracker.EnderNodeTracker
import at.hannibal2.skyhanni.features.combat.ghostcounter.GhostUtil
import at.hannibal2.skyhanni.features.commands.HelpCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import at.hannibal2.skyhanni.config.FeatureToggle;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.Accordion;
import io.github.notenoughupdates.moulconfig.annotations.Category;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorDraggableList;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorKeybind;
Expand Down Expand Up @@ -104,16 +105,9 @@ public String toString() {
@FeatureToggle
public boolean hideSacksChange = false;

@Category(name = "Translator", desc = "Chat translator settings.")
@Expose
@ConfigOption(
name = "Translator",
desc = "Click on a message to translate it to English.\n" +
"Use §e/shcopytranslation§7 to translate from English.\n" +
"§cTranslation is not guaranteed to be 100% accurate."
)
@ConfigEditorBoolean
@FeatureToggle
public boolean translator = false;
public TranslatorConfig translator = new TranslatorConfig();

@Expose
@ConfigOption(name = "SkyBlock XP in Chat", desc = "Send the SkyBlock XP messages into the chat.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package at.hannibal2.skyhanni.config.features.chat;

import at.hannibal2.skyhanni.config.FeatureToggle;
import at.hannibal2.skyhanni.features.chat.translation.TranslatableLanguage;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorDropdown;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorText;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;
import io.github.notenoughupdates.moulconfig.observer.Property;

public class TranslatorConfig {

@Expose
@ConfigOption(
name = "Translate On Click",
desc = "Click on a message to translate it to English.\n" +
"Use §e/shcopytranslation§7 to translate from English.\n" +
"§cTranslation is not guaranteed to be 100% accurate."
)
@ConfigEditorBoolean
@FeatureToggle
public boolean translateOnClick = false;

@ConfigOption(name = "Language Name", desc = "The name of the language selected below. Note that languages marked as unknown might still be supported.")
@Expose
@ConfigEditorDropdown
public Property<TranslatableLanguage> languageName = Property.of(TranslatableLanguage.ENGLISH);

@Expose
@ConfigOption(
name = "Language Code",
desc = "Enter a language code here to translate on chat click into another language. " +
"E.g. `es` for spanish or 'de' for german. Empty for english.")
@ConfigEditorText
public Property<String> languageCode = Property.of("en");
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -44,14 +49,49 @@ 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"))
style.setChatHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatComponentText("§bClick to translate!")))
return style
}

private val config get() = SkyHanniMod.feature.chat
private val config get() = SkyHanniMod.feature.chat.translator

/*
* Simplified version of the JSON response:
Expand Down Expand Up @@ -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")
}
}

Expand All @@ -151,5 +204,5 @@ object Translator {
}
}

fun isEnabled() = config.translator
fun isEnabled() = config.translateOnClick
}

0 comments on commit a658f0f

Please sign in to comment.