-
-
Notifications
You must be signed in to change notification settings - Fork 46
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 #347 from you-apps/mozhi
feat: support for mozhi
- Loading branch information
Showing
11 changed files
with
155 additions
and
35 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
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 | ||
) | ||
} | ||
} |
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,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
27
app/src/main/java/com/bnyro/translate/api/mh/obj/MhLanguage.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,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 | ||
) |
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
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