Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,36 @@ import net.ccbluex.liquidbounce.api.thirdparty.translator.TranslatorApi
import net.ccbluex.liquidbounce.api.thirdparty.translator.providers.GoogleTranslateApi
import net.ccbluex.liquidbounce.config.types.group.ValueGroup
import net.ccbluex.liquidbounce.event.EventListener
import net.ccbluex.liquidbounce.utils.collection.LruCache

private data class TranslationKey(val sourceLanguage: String, val targetLanguage: String, val text: String)

object GlobalSettingsAutoTranslate : ValueGroup(name = "AutoTranslate"), TranslatorApi, EventListener {

private val cache = LruCache<TranslationKey, TranslationResult>(10_000)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10_000 is incorrect. Should be power of 2. You can probably use Guava's cache.

private val providers = modes(this, "Provider", 0) {
arrayOf(
GoogleTranslateApi(it)
)
}

override suspend fun translate(
sourceLanguage: TranslateLanguage,
targetLanguage: TranslateLanguage,
text: String
): TranslationResult {
val key = TranslationKey(sourceLanguage.literal, targetLanguage.literal, text)
cache[key]?.let { return it }

val result = super.translate(sourceLanguage, targetLanguage, text)

if (result.isValid) {
cache.put(key, result)
}

return result
}

override suspend fun translateInternal(
sourceLanguage: TranslateLanguage,
targetLanguage: TranslateLanguage,
Expand Down