forked from NextAlone/Nagram
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
56 additions
and
0 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
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
47 changes: 47 additions & 0 deletions
47
TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/LibreTranslator.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,47 @@ | ||
package tw.nekomimi.nekogram.transtale.source | ||
|
||
import cn.hutool.http.HttpUtil | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import org.telegram.messenger.LocaleController | ||
import org.telegram.messenger.R | ||
import tw.nekomimi.nekogram.transtale.Translator | ||
import tw.nekomimi.nekogram.utils.applyUserAgent | ||
import java.util.Date | ||
import java.util.UUID | ||
|
||
object LibreTranslator : Translator { | ||
|
||
private val targetLanguages = listOf( | ||
"sq", "ar", "az", "bn", "bg", "ca", "zh", "zt", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "de", "el", "he", "hi", "hu", "id", "ga", "it", "ja", "ko", "lv", "lt", "ms", "nb", "fa", "pl", "pt", "ro", "ru", "sr", "sk", "sl", "es", "sv", "tl", "th", "tr", "uk", "ur", "vi" | ||
) | ||
|
||
override suspend fun doTranslate(from: String, to: String, query: String): String { | ||
|
||
if (to !in targetLanguages) { | ||
error(LocaleController.getString("TranslateApiUnsupported", R.string.TranslateApiUnsupported)) | ||
} | ||
|
||
val response = HttpUtil.createPost("https://libretranslate.com/translate") | ||
.header("Content-Type", "application/json") | ||
.applyUserAgent() | ||
.body(JSONObject().apply { | ||
put("q", query) | ||
put("source", if (targetLanguages.contains(from)) from else "en") | ||
put("target", to) | ||
put("format", "text") | ||
put("api_key", "") | ||
}.toString()) | ||
.execute() | ||
|
||
if (response.status != 200) { | ||
error("HTTP ${response.status} : ${response.body()}") | ||
} | ||
|
||
val target: JSONArray = JSONObject(response.body()).getJSONArray("translatedText") | ||
|
||
return target.toString().trimEnd() | ||
|
||
} | ||
|
||
} |
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