-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from DimensionDev/feature/google_translate
replace mlkit translate with google translate
- Loading branch information
Showing
6 changed files
with
62 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
shared/src/commonMain/kotlin/dev/dimension/flare/ui/presenter/status/TranslatePresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |