From c467171c97e2502dbdd3b39794be88f71bb1fa0f Mon Sep 17 00:00:00 2001 From: LiuYi0526 <50787709+LiuYi0526@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:59:17 +0800 Subject: [PATCH] feat: add LibreTranslate --- .../settings/NekoGeneralSettingsActivity.java | 4 ++ .../nekomimi/nekogram/transtale/Translator.kt | 2 + .../transtale/source/LibreTranslator.kt | 45 +++++++++++++++++++ .../src/main/res/values-zh-rCN/strings_na.xml | 1 + .../src/main/res/values-zh-rTW/strings_na.xml | 1 + .../src/main/res/values/strings_na.xml | 1 + 6 files changed, 54 insertions(+) create mode 100644 TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/LibreTranslator.kt diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java index 0537f7b61e..943580b925 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoGeneralSettingsActivity.java @@ -354,6 +354,7 @@ public void onItemClick(int id) { LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate), LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI), LocaleController.getString("ProviderTranSmartTranslate", R.string.ProviderTranSmartTranslate), + LocaleController.getString("ProviderLibreTranslateTranslate", R.string.ProviderLibreTranslateTranslate), }, (i, __) -> { boolean needReset = NekoConfig.translationProvider.Int() - 1 != i && (NekoConfig.translationProvider.Int() == 1 || i == 0); NekoConfig.translationProvider.setConfigInt(i + 1); @@ -769,6 +770,9 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { case Translator.providerTranSmart: value = LocaleController.getString("ProviderTranSmartTranslate", R.string.ProviderTranSmartTranslate); break; + case Translator.providerLibreTranslate: + value = LocaleController.getString("ProviderLibreTranslateTranslate", R.string.ProviderLibreTranslateTranslate); + break; default: value = "Unknown"; } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt index aa4479197c..ca175afc99 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt @@ -83,6 +83,7 @@ interface Translator { const val providerDeepL = 7 const val providerTelegram = 8 const val providerTranSmart = 9 + const val providerLibreTranslate = 10 @Throws(Exception::class) suspend fun translate(to: Locale, query: String): String { @@ -121,6 +122,7 @@ interface Translator { providerDeepL -> DeepLTranslator providerTelegram -> TelegramAPITranslator providerTranSmart -> TranSmartTranslator + providerLibreTranslate -> LibreTranslator else -> throw IllegalArgumentException() } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/LibreTranslator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/LibreTranslator.kt new file mode 100644 index 0000000000..546d4a9ef9 --- /dev/null +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/LibreTranslator.kt @@ -0,0 +1,45 @@ +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 + +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://translate.fedilab.app/translate") + .header("Content-Type", "application/json") + .applyUserAgent() + .body(JSONObject().apply { + put("q", query) + put("source", if (targetLanguages.contains(from)) from else "auto") + put("target", to) + put("format", "text") + put("api_key", "") + }.toString()) + .execute() + + if (response.status != 200) { + error("HTTP ${response.status} : ${response.body()}") + } + + val target = JSONObject(response.body()).getString("translatedText") + + return target + + } + +} diff --git a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml index 3aeb9d4fa2..179f4336e4 100644 --- a/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml +++ b/TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml @@ -100,4 +100,5 @@ 刷新外部缓存 删除外部缓存 腾讯交互翻译 + LibreTranslate diff --git a/TMessagesProj/src/main/res/values-zh-rTW/strings_na.xml b/TMessagesProj/src/main/res/values-zh-rTW/strings_na.xml index 00b2890c1f..518635ede0 100644 --- a/TMessagesProj/src/main/res/values-zh-rTW/strings_na.xml +++ b/TMessagesProj/src/main/res/values-zh-rTW/strings_na.xml @@ -79,4 +79,5 @@ 隱藏輸入框中的機器人按鈕 在訊息提示下顯示傳送者 騰訊交互翻譯 + LibreTranslate diff --git a/TMessagesProj/src/main/res/values/strings_na.xml b/TMessagesProj/src/main/res/values/strings_na.xml index 08c189c1a2..179b0242f4 100644 --- a/TMessagesProj/src/main/res/values/strings_na.xml +++ b/TMessagesProj/src/main/res/values/strings_na.xml @@ -103,4 +103,5 @@ Refresh External Cache Delete External Cache TranSmart Translator + LibreTranslate