Skip to content

Commit

Permalink
Merge pull request #638 from DimensionDev/feature/google_translate
Browse files Browse the repository at this point in the history
replace mlkit translate with google translate
  • Loading branch information
Tlaster authored Jan 7, 2025
2 parents bc5ac37 + e203d81 commit c69d502
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 72 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ dependencies {
implementation(libs.androidx.window)
coreLibraryDesugaring(libs.desugar.jdk.libs)
implementation(libs.compose.webview)
implementation(libs.mlkit.translate)
releaseImplementation(libs.mlkit.language.id)
debugImplementation(libs.mlkit.language.id.debug)
implementation(libs.kotlinx.coroutines.play.services)
implementation(projects.shared)
implementation(projects.shared.ui)
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="langid" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import dev.dimension.flare.ui.presenter.status.StatusPresenter
import dev.dimension.flare.ui.screen.home.NavigationState
import dev.dimension.flare.ui.theme.FlareTheme
import io.github.fornewid.placeholder.material3.placeholder
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -106,7 +107,6 @@ import me.saket.telephoto.zoomable.rememberZoomableState
import moe.tlaster.precompose.molecule.producePresenter
import moe.tlaster.swiper.Swiper
import moe.tlaster.swiper.rememberSwiperState
import okhttp3.internal.toImmutableList
import org.koin.compose.koinInject
import soup.compose.material.motion.animation.materialFadeThroughIn
import soup.compose.material.motion.animation.materialFadeThroughOut
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package dev.dimension.flare.ui.screen.status

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import com.fleeksoft.ksoup.nodes.Element
import com.google.mlkit.nl.languageid.LanguageIdentification
import com.google.mlkit.nl.translate.TranslateLanguage
import com.google.mlkit.nl.translate.Translation
import com.google.mlkit.nl.translate.TranslatorOptions
import dev.dimension.flare.ui.model.UiState
import dev.dimension.flare.ui.model.flatMap
import dev.dimension.flare.ui.presenter.invoke
import dev.dimension.flare.ui.presenter.status.TranslatePresenter
import dev.dimension.flare.ui.render.UiRichText
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.tasks.await

@Composable
fun statusTranslatePresenter(
Expand All @@ -35,55 +27,7 @@ fun statusTranslatePresenter(
@Composable
private fun translateText(text: String) =
run {
val language by remember(text) {
flow<UiState<String>> {
runCatching {
LanguageIdentification
.getClient()
.identifyLanguage(text)
.await()
}.fold(
onSuccess = { emit(UiState.Success(it)) },
onFailure = { emit(UiState.Error(it)) },
)
}
}.collectAsState(UiState.Loading())
language.flatMap {
val source =
remember(it) {
TranslateLanguage.fromLanguageTag(it)
}
val locale = androidx.compose.ui.text.intl.Locale.current
val target =
remember {
TranslateLanguage.fromLanguageTag(locale.language)
}
if (source == target || source == null || target == null) {
UiState.Success(text)
} else {
remember(source, target) {
val options =
TranslatorOptions
.Builder()
.setSourceLanguage(source)
.setTargetLanguage(target)
.build()
val client =
Translation.getClient(options)
flow<UiState<String>> {
runCatching {
client
.downloadModelIfNeeded()
.await()
client
.translate(text)
.await()
}.fold(
onSuccess = { emit(UiState.Success(it)) },
onFailure = { emit(UiState.Error(it)) },
)
}
}.collectAsState(UiState.Loading()).value
}
}
remember(text) {
TranslatePresenter(text)
}.invoke()
}
4 changes: 0 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.r
firebase-analytics-ktx = { group = "com.google.firebase", name = "firebase-analytics-ktx" }
firebase-crashlytics-ktx = { group = "com.google.firebase", name = "firebase-crashlytics-ktx" }

mlkit-language-id-debug = { group = "com.google.mlkit", name = "language-id", version = "17.0.6" }
mlkit-language-id = { group = "com.google.android.gms", name = "play-services-mlkit-language-id", version = "17.0.0" }
mlkit-translate = { group = "com.google.mlkit", name = "translate", version = "17.0.3" }

materialKolor = { module = "com.materialkolor:material-kolor", version.ref = "materialKolor" }
colorpicker-compose = { module = "com.github.skydoves:colorpicker-compose", version = "1.1.2" }
material-motion-compose = { module = "io.github.fornewid:material-motion-compose-core", version = "1.2.1" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.dimension.flare.ui.presenter.status

import androidx.compose.runtime.Composable
import androidx.compose.runtime.produceState
import dev.dimension.flare.common.Locale
import dev.dimension.flare.data.network.ktorClient
import dev.dimension.flare.ui.model.UiState
import dev.dimension.flare.ui.presenter.PresenterBase
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.url
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonPrimitive

public class TranslatePresenter(
private val source: String,
private val targetLanguage: String = Locale.language,
) : PresenterBase<UiState<String>>() {
@Composable
override fun body(): UiState<String> {
val baseUrl = "https://translate.google.com/translate_a/single"
return produceState(UiState.Loading()) {
value =
runCatching {
val response =
ktorClient()
.get {
url(baseUrl)
parameter("client", "gtx")
parameter("sl", "auto")
parameter("tl", targetLanguage)
parameter("dt", "t")
parameter("q", source)
parameter("ie", "UTF-8")
parameter("oe", "UTF-8")
}.body<JsonArray>()
buildString {
response.firstOrNull()?.jsonArray?.forEach {
it.jsonArray.firstOrNull()?.let {
val content = it.jsonPrimitive.content
if (content.isNotEmpty()) {
append(content)
append("\n")
}
}
}
}
}.fold(
onSuccess = { UiState.Success(it) },
onFailure = { UiState.Error(it) },
)
}.value
}
}

0 comments on commit c69d502

Please sign in to comment.