Skip to content

Commit

Permalink
feat: implement one ring translator engine (closes #393)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jan 5, 2024
1 parent d18aedf commit 9aa1dbd
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
33 changes: 33 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/or/OneRing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 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.or

import com.bnyro.translate.api.or.obj.OneRingResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface OneRing {
@GET("translate")
suspend fun translate(
@Query("text") text: String,
@Query("from_lang") from: String,
@Query("to") to: String,
@Query("translator_plugin") plugin: String,
@Query("api_key") apiKey: String?
): OneRingResponse
}
78 changes: 78 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/or/OneRingEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 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.or

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
import java.util.*

class OneRingEngine: TranslationEngine(
name = "OneRing",
apiKeyState = ApiKeyState.OPTIONAL,
urlModifiable = true,
defaultUrl = "https://your.instance.domain",
autoLanguageCode = "",
supportedEngines = listOf(
"no_translate2",
"no_translate",
"fb_nllb_ctranslate2",
"bloomz",
"vsegpt_chat",
"fb_nllb_translate",
"opus_mt",
"google_translate",
"deepl",
"deepl_translate",
"use_mid_lang",
"fb_mbart50",
"openai_chat",
"libre_translate",
"koboldapi_translate",
"lingvanex",
"multi_sources"
)
) {
private lateinit var api: OneRing

override fun createOrRecreate(): TranslationEngine = apply {
api = RetrofitHelper.createApi(this)
}

override suspend fun getLanguages(): List<Language> {
return Locale.getAvailableLocales().map {
Language(it.isO3Language, it.getDisplayName(Locale.getDefault()))
}
.distinctBy { it.code }
.sortedBy { it.name }
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val response = api.translate(
text = query,
from = source,
to = target,
apiKey = getApiKey(),
plugin = getSelectedEngine()
)

return Translation(response.result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 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.or.obj

import kotlinx.serialization.Serializable

@Serializable
data class OneRingResponse(
val result: String,
val cache: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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.or.OneRingEngine
import com.bnyro.translate.api.reverso.ReversoEngine
import com.bnyro.translate.api.st.STEngine
import com.bnyro.translate.api.wm.WmEngine
Expand All @@ -39,7 +40,8 @@ object TranslationEngines {
MhEngine(),
WmEngine(),
GlEngine(),
ApEngine()
ApEngine(),
OneRingEngine()
).map {
it.createOrRecreate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fun EnginePref() {
LazyColumn {
items(engine.supportedEngines) { availableEngine ->
SelectableItem(
text = availableEngine.capitalize()
text = availableEngine.replace("_", " ").capitalize()
) {
Preferences.put(engine.selEnginePrefKey, availableEngine)
engine.createOrRecreate()
Expand Down

0 comments on commit 9aa1dbd

Please sign in to comment.