diff --git a/app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt b/app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt new file mode 100644 index 000000000..436a2057a --- /dev/null +++ b/app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 You Apps + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.bnyro.translate.api.mh + +import com.bnyro.translate.const.ApiKeyState +import com.bnyro.translate.db.obj.Language +import com.bnyro.translate.obj.Translation +import com.bnyro.translate.util.RetrofitHelper +import com.bnyro.translate.util.TranslationEngine + +class MhEngine : TranslationEngine( + name = "Mozhi", + defaultUrl = "https://mozhi.aryak.me/", + urlModifiable = true, + apiKeyState = ApiKeyState.DISABLED, + autoLanguageCode = "auto", + supportedEngines = listOf( + "google", + "libre", + "reverso", + "deepl", + "duckduckgo", + "mymemory", + "watson", + "yandex" + ) +) { + lateinit var api: Mozhi + + + override fun createOrRecreate(): TranslationEngine = apply { + api = RetrofitHelper.createApi(this) + } + + override suspend fun getLanguages(): List { + return api.getLanguages(getSelectedEngine()) + .map { Language(it.id, it.name) } + } + + override suspend fun translate(query: String, source: String, target: String): Translation { + val response = api.translate( + engine = getSelectedEngine(), + source = sourceOrAuto(source), + query = query, + target = target + ) + return Translation( + translatedText = response.translatedText, + detectedLanguage = response.sourceLanguage + ) + } +} diff --git a/app/src/main/java/com/bnyro/translate/api/mh/Mozhi.kt b/app/src/main/java/com/bnyro/translate/api/mh/Mozhi.kt new file mode 100644 index 000000000..c929bece1 --- /dev/null +++ b/app/src/main/java/com/bnyro/translate/api/mh/Mozhi.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 You Apps + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.bnyro.translate.api.mh + +import com.bnyro.translate.api.mh.obj.MhLanguage +import com.bnyro.translate.api.st.obj.STTranslationResponse +import retrofit2.http.GET +import retrofit2.http.Query + +interface Mozhi { + @GET("api/translate/") + suspend fun translate( + @Query("engine") engine: String? = null, + @Query("from") source: String, + @Query("to") target: String, + @Query("text") query: String + ): STTranslationResponse + + @GET("api/target_languages/") + suspend fun getLanguages( + @Query("engine") engine: String? + ): List +} diff --git a/app/src/main/java/com/bnyro/translate/api/mh/obj/MhLanguage.kt b/app/src/main/java/com/bnyro/translate/api/mh/obj/MhLanguage.kt new file mode 100644 index 000000000..84e207d63 --- /dev/null +++ b/app/src/main/java/com/bnyro/translate/api/mh/obj/MhLanguage.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 You Apps + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.bnyro.translate.api.mh.obj + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class MhLanguage( + @SerialName("Id") val id: String, + @SerialName("Name") val name: String +) diff --git a/app/src/main/java/com/bnyro/translate/api/st/STEngine.kt b/app/src/main/java/com/bnyro/translate/api/st/STEngine.kt index 68ee2bea3..a0233186c 100644 --- a/app/src/main/java/com/bnyro/translate/api/st/STEngine.kt +++ b/app/src/main/java/com/bnyro/translate/api/st/STEngine.kt @@ -30,10 +30,10 @@ class STEngine : TranslationEngine( defaultUrl = "https://simplytranslate.org/", urlModifiable = true, apiKeyState = ApiKeyState.DISABLED, - autoLanguageCode = "auto" + autoLanguageCode = "auto", + supportedEngines = listOf("google", "libre", "reverso", "iciba") ) { lateinit var api: SimplyTranslate - val selEnginePrefKey = this.name + "selectedEngine" override fun createOrRecreate(): TranslationEngine = apply { api = RetrofitHelper.createApi(this) @@ -64,17 +64,8 @@ class STEngine : TranslationEngine( target = target ) return Translation( - translatedText = response.translated_text, - detectedLanguage = response.source_language + translatedText = response.translatedText, + detectedLanguage = response.sourceLanguage ) } - - private fun getSelectedEngine(): String? { - Preferences.get(selEnginePrefKey, "all").let { - return when (it) { - "all" -> null - else -> it - } - } - } } diff --git a/app/src/main/java/com/bnyro/translate/api/st/obj/STTranslationResponse.kt b/app/src/main/java/com/bnyro/translate/api/st/obj/STTranslationResponse.kt index 2200e4857..9d6fbf109 100644 --- a/app/src/main/java/com/bnyro/translate/api/st/obj/STTranslationResponse.kt +++ b/app/src/main/java/com/bnyro/translate/api/st/obj/STTranslationResponse.kt @@ -25,7 +25,7 @@ data class STTranslationResponse( @SerialName("definitions") val definitions: STDefinition? = null, @SerialName("source_language") - val source_language: String? = null, + val sourceLanguage: String? = null, @SerialName("translated-text") - val translated_text: String = "" + val translatedText: String = "" ) diff --git a/app/src/main/java/com/bnyro/translate/const/TranslationEngines.kt b/app/src/main/java/com/bnyro/translate/const/TranslationEngines.kt index 1bcda0b38..4a106d5eb 100644 --- a/app/src/main/java/com/bnyro/translate/const/TranslationEngines.kt +++ b/app/src/main/java/com/bnyro/translate/const/TranslationEngines.kt @@ -22,6 +22,7 @@ import com.bnyro.translate.api.deepl.DeeplEngine import com.bnyro.translate.api.gl.GlEngine import com.bnyro.translate.api.lt.LTEngine import com.bnyro.translate.api.lv.LVEngine +import com.bnyro.translate.api.mh.MhEngine import com.bnyro.translate.api.mm.MMEngine import com.bnyro.translate.api.reverso.ReversoEngine import com.bnyro.translate.api.st.STEngine @@ -35,6 +36,7 @@ object TranslationEngines { MMEngine(), ReversoEngine(), STEngine(), + MhEngine(), WmEngine(), GlEngine(), ApEngine() diff --git a/app/src/main/java/com/bnyro/translate/ui/components/BlockRadioButton.kt b/app/src/main/java/com/bnyro/translate/ui/components/BlockRadioButton.kt index 692b01b44..f04299dcf 100644 --- a/app/src/main/java/com/bnyro/translate/ui/components/BlockRadioButton.kt +++ b/app/src/main/java/com/bnyro/translate/ui/components/BlockRadioButton.kt @@ -38,7 +38,7 @@ fun BlockRadioButton( Column { LazyVerticalGrid( columns = GridCells.Fixed(3), - modifier = Modifier.heightIn(max = 200.dp) + modifier = Modifier.heightIn(max = 300.dp) ) { items(items) { val index = items.indexOf(it) diff --git a/app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt b/app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt index 63b860f84..cf31a2f12 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt @@ -25,6 +25,7 @@ import androidx.compose.foundation.lazy.items import androidx.compose.material3.AlertDialog import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -33,7 +34,6 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.bnyro.translate.R import com.bnyro.translate.api.deepl.DeeplEngine -import com.bnyro.translate.api.st.STEngine import com.bnyro.translate.const.ApiKeyState import com.bnyro.translate.const.TranslationEngines import com.bnyro.translate.ext.capitalize @@ -49,15 +49,8 @@ import com.bnyro.translate.util.Preferences fun EnginePref() { val engines = TranslationEngines.engines - Log.e("engines", engines.map { it.name }.toString()) - var selected by remember { - mutableStateOf( - Preferences.get( - Preferences.apiTypeKey, - 0 - ) - ) + mutableIntStateOf(Preferences.get(Preferences.apiTypeKey, 0)) } var instanceUrl by remember { @@ -117,10 +110,8 @@ fun EnginePref() { } } - when (engine) { - is STEngine -> { - val avEngines = listOf("all", "google", "libre", "reverso", "iciba") - + when { + engine.supportedEngines.isNotEmpty() -> { var showEngineSelDialog by remember { mutableStateOf(false) } @@ -149,11 +140,11 @@ fun EnginePref() { }, text = { LazyColumn { - items(avEngines) { usedEngine -> + items(engine.supportedEngines) { availableEngine -> SelectableItem( - text = usedEngine.capitalize() + text = availableEngine.capitalize() ) { - Preferences.put(engine.selEnginePrefKey, usedEngine) + Preferences.put(engine.selEnginePrefKey, availableEngine) engine.createOrRecreate() showEngineSelDialog = false } @@ -163,7 +154,7 @@ fun EnginePref() { ) } } - is DeeplEngine -> { + engine is DeeplEngine -> { Spacer(modifier = Modifier.height(5.dp)) SwitchPreference( preferenceKey = engine.useFreeApiKey, diff --git a/app/src/main/java/com/bnyro/translate/util/Preferences.kt b/app/src/main/java/com/bnyro/translate/util/Preferences.kt index bb0c08bf0..067c84d65 100644 --- a/app/src/main/java/com/bnyro/translate/util/Preferences.kt +++ b/app/src/main/java/com/bnyro/translate/util/Preferences.kt @@ -35,6 +35,7 @@ object Preferences { const val appLanguageKey = "appLanguage" const val charCounterLimitKey = "charCountLimit" const val tessLanguageKey = "tessLanguage" + const val selectedEngine = "selectedEngine" const val themeModeKey = "themeModeKey" const val accentColorKey = "accentColor" diff --git a/app/src/main/java/com/bnyro/translate/util/RetrofitHelper.kt b/app/src/main/java/com/bnyro/translate/util/RetrofitHelper.kt index f0c4b4a07..eaeac0dfd 100644 --- a/app/src/main/java/com/bnyro/translate/util/RetrofitHelper.kt +++ b/app/src/main/java/com/bnyro/translate/util/RetrofitHelper.kt @@ -27,7 +27,6 @@ import retrofit2.Retrofit import retrofit2.converter.scalars.ScalarsConverterFactory object RetrofitHelper { - @OptIn(ExperimentalSerializationApi::class) inline fun createApi(engine: TranslationEngine): T { val baseUrl = engine.getUrl() diff --git a/app/src/main/java/com/bnyro/translate/util/TranslationEngine.kt b/app/src/main/java/com/bnyro/translate/util/TranslationEngine.kt index e58411a9f..3605abddd 100644 --- a/app/src/main/java/com/bnyro/translate/util/TranslationEngine.kt +++ b/app/src/main/java/com/bnyro/translate/util/TranslationEngine.kt @@ -30,7 +30,8 @@ abstract class TranslationEngine( val apiKeyState: ApiKeyState, val autoLanguageCode: String?, val supportsSimTranslation: Boolean = true, - val supportsAudio: Boolean = false + val supportsAudio: Boolean = false, + val supportedEngines: List = emptyList(), ) { abstract fun createOrRecreate(): TranslationEngine @@ -42,6 +43,7 @@ abstract class TranslationEngine( val urlPrefKey = this.name + Preferences.instanceUrlKey val apiPrefKey = this.name + Preferences.apiKey val simPrefKey = this.name + Preferences.simultaneousTranslationKey + val selEnginePrefKey = this.name + Preferences.selectedEngine open fun getUrl(): String { return Preferences.get( @@ -65,4 +67,6 @@ abstract class TranslationEngine( simPrefKey, false ) + + fun getSelectedEngine() = Preferences.get(selEnginePrefKey, supportedEngines.first()) }