Skip to content

Commit

Permalink
fix translation crash
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Jul 11, 2024
1 parent 3274d21 commit 0fe71ed
Showing 1 changed file with 49 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.google.mlkit.nl.translate.TranslateLanguage
import com.google.mlkit.nl.translate.Translation
import com.google.mlkit.nl.translate.TranslatorOptions
import dev.datlag.aniflow.SharedRes
import dev.datlag.tooling.scopeCatching
import dev.icerock.moko.resources.compose.stringResource
import java.util.Locale

Expand All @@ -34,27 +35,37 @@ actual fun TranslateButton(
}

val targetLanguage = remember(locale) {
TranslateLanguage.fromLanguageTag(locale.toLanguageTag())
?: TranslateLanguage.fromLanguageTag(locale.language)
?: TranslateLanguage.fromLanguageTag(locale.isO3Language)
scopeCatching {
TranslateLanguage.fromLanguageTag(locale.toLanguageTag())
?: TranslateLanguage.fromLanguageTag(locale.language)
?: TranslateLanguage.fromLanguageTag(locale.isO3Language)
}.getOrNull()
}

if (targetLanguage == null || targetLanguage == TranslateLanguage.ENGLISH) {
return
}

val options = remember(targetLanguage) {
TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(targetLanguage)
.build()
}
val englishLocaleTranslator = remember(options) { Translation.getClient(options) }
scopeCatching {
TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(targetLanguage)
.build()
}.getOrNull()
} ?: return
val englishLocaleTranslator = remember(options) {
scopeCatching {
Translation.getClient(options)
}.getOrNull()
} ?: return
val downloadConditions = remember {
DownloadConditions.Builder()
.requireWifi()
.build()
}
scopeCatching {
DownloadConditions.Builder()
.requireWifi()
.build()
}.getOrNull()
} ?: return
var enabled by remember(text) { mutableStateOf(text.isNotBlank()) }
var translated by remember { mutableStateOf(false) }
var progress by remember { mutableStateOf(false) }
Expand All @@ -68,28 +79,30 @@ actual fun TranslateButton(
progress = true
enabled = false

englishLocaleTranslator
.downloadModelIfNeeded(downloadConditions)
.addOnFailureListener {
progress = false
enabled = false
}.addOnSuccessListener {
englishLocaleTranslator
.translate(text)
.addOnFailureListener {
progress = false
enabled = true
scopeCatching {
englishLocaleTranslator
.downloadModelIfNeeded(downloadConditions)
.addOnFailureListener {
progress = false
enabled = false
}.addOnSuccessListener {
englishLocaleTranslator
.translate(text)
.addOnFailureListener {
progress = false
enabled = true

translated = false
onTranslation(null)
}.addOnSuccessListener {
progress = false
enabled = true
translated = false
onTranslation(null)
}.addOnSuccessListener {
progress = false
enabled = true

translated = true
onTranslation(it)
}
}
translated = true
onTranslation(it)
}
}
}.getOrNull()
}
},
enabled = enabled
Expand All @@ -113,7 +126,9 @@ actual fun TranslateButton(

DisposableEffect(englishLocaleTranslator) {
onDispose {
englishLocaleTranslator.close()
scopeCatching {
englishLocaleTranslator.close()
}.getOrNull()
}
}
}

0 comments on commit 0fe71ed

Please sign in to comment.