Skip to content

Commit

Permalink
feat: support for translation engine PONS
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Apr 9, 2024
1 parent f730dc9 commit 627bae6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
38 changes: 38 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/po/Pons.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.po

import com.bnyro.translate.api.po.obj.PonsData
import kotlinx.serialization.json.JsonObject
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query

interface Pons {
@GET("text-translation-web/v4/languages")
suspend fun getLanguages(
@Query("locale") locale: String = "en"
): JsonObject

@POST("text-translation-web/v4/translate")
suspend fun translate(
@Query("locale") locale: String = "en",
@Body body: PonsData
): PonsData
}
56 changes: 56 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/po/PonsEngine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.po

import com.bnyro.translate.api.po.obj.PonsData
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.lang.IllegalArgumentException
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

class PonsEngine : TranslationEngine(
name = "Pons",
defaultUrl = "https://api.pons.com",
urlModifiable = false,
apiKeyState = ApiKeyState.DISABLED,
autoLanguageCode = ""
) {
private lateinit var api: Pons

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

override suspend fun getLanguages(): List<Language> {
return api.getLanguages()["languages"]?.jsonObject?.map {
val langName = it.value.jsonObject["display"]?.jsonPrimitive?.content ?: throw IllegalArgumentException()
Language(it.key, langName)
}?.sortedBy { it.name } ?: throw IllegalArgumentException()
}

override suspend fun translate(query: String, source: String, target: String): Translation {
val requestBody = PonsData(source.takeIf { it.isNotEmpty() }, target, query)
val response = api.translate(body = requestBody)

return Translation(response.text, response.sourceLanguage)
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/po/obj/PonsData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.po.obj

import kotlinx.serialization.Serializable

@Serializable
data class PonsData(
val sourceLanguage: String?,
val targetLanguage: String,
val text: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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.po.PonsEngine
import com.bnyro.translate.api.reverso.ReversoEngine
import com.bnyro.translate.api.st.STEngine
import com.bnyro.translate.api.wm.WmEngine
Expand All @@ -41,7 +42,8 @@ object TranslationEngines {
WmEngine(),
GlEngine(),
ApEngine(),
OneRingEngine()
OneRingEngine(),
PonsEngine()
).map {
it.createOrRecreate()
}
Expand Down

0 comments on commit 627bae6

Please sign in to comment.