Skip to content

Commit

Permalink
Merge pull request #347 from you-apps/mozhi
Browse files Browse the repository at this point in the history
feat: support for mozhi
  • Loading branch information
Bnyro authored Sep 29, 2023
2 parents 9ab0acb + 7b45d93 commit 0d600c4
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 35 deletions.
67 changes: 67 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<Language> {
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
)
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/mh/Mozhi.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<MhLanguage>
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/mh/obj/MhLanguage.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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
)
17 changes: 4 additions & 13 deletions app/src/main/java/com/bnyro/translate/api/st/STEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,7 @@ object TranslationEngines {
MMEngine(),
ReversoEngine(),
STEngine(),
MhEngine(),
WmEngine(),
GlEngine(),
ApEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 8 additions & 17 deletions app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -163,7 +154,7 @@ fun EnginePref() {
)
}
}
is DeeplEngine -> {
engine is DeeplEngine -> {
Spacer(modifier = Modifier.height(5.dp))
SwitchPreference(
preferenceKey = engine.useFreeApiKey,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/bnyro/translate/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory

object RetrofitHelper {
@OptIn(ExperimentalSerializationApi::class)
inline fun <reified T> createApi(engine: TranslationEngine): T {
val baseUrl = engine.getUrl()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = emptyList(),
) {

abstract fun createOrRecreate(): TranslationEngine
Expand All @@ -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(
Expand All @@ -65,4 +67,6 @@ abstract class TranslationEngine(
simPrefKey,
false
)

fun getSelectedEngine() = Preferences.get(selEnginePrefKey, supportedEngines.first())
}

0 comments on commit 0d600c4

Please sign in to comment.