Skip to content

Commit

Permalink
feat: add LibreTranslate
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYi0526 committed Jan 31, 2024
1 parent 43353aa commit 4ac21fa
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -121,6 +122,7 @@ interface Translator {
providerDeepL -> DeepLTranslator
providerTelegram -> TelegramAPITranslator
providerTranSmart -> TranSmartTranslator
providerLibreTranslate -> LibreTranslator
else -> throw IllegalArgumentException()
}

Expand Down
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://translate.fedilab.app/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 = JSONObject(response.body()).getJSONArray("translatedText")

return target.toString().trimEnd()

}

}
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh-rCN/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@
<string name="ExternalStickerCacheRefresh">刷新外部缓存</string>
<string name="ExternalStickerCacheDelete">删除外部缓存</string>
<string name="ProviderTranSmartTranslate">腾讯交互翻译</string>
<string name="ProviderLibreTranslateTranslate">LibreTranslate</string>
</resources>
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh-rTW/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@
<string name="HideBotButtonInInputField">隱藏輸入框中的機器人按鈕</string>
<string name="ShowSendAsUnderMessageHint">在訊息提示下顯示傳送者</string>
<string name="ProviderTranSmartTranslate">騰訊交互翻譯</string>
<string name="ProviderLibreTranslateTranslate">LibreTranslate</string>
</resources>
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values/strings_na.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@
<string name="ExternalStickerCacheRefresh">Refresh External Cache</string>
<string name="ExternalStickerCacheDelete">Delete External Cache</string>
<string name="ProviderTranSmartTranslate">TranSmart Translator</string>
<string name="ProviderLibreTranslateTranslate">LibreTranslate</string>
</resources>

0 comments on commit 4ac21fa

Please sign in to comment.