feat: add TranSmartTranslator

Co-authored-by: Naomi <mangogo.naomi@qq.com>
Co-authored-by: qwq233 <qwq233@qwq2333.top>
This commit is contained in:
xtaodada 2024-01-30 21:04:41 +08:00
parent 0e2092d15e
commit 7b5e705e7c
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
6 changed files with 93 additions and 1 deletions

View File

@ -352,7 +352,8 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderMicrosoftTranslator),
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderYouDao),
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate),
LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI)
LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI),
LocaleController.getString("ProviderTranSmartTranslate", R.string.ProviderTranSmartTranslate),
}, (i, __) -> {
boolean needReset = NekoConfig.translationProvider.Int() - 1 != i && (NekoConfig.translationProvider.Int() == 1 || i == 0);
NekoConfig.translationProvider.setConfigInt(i + 1);
@ -765,6 +766,9 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
case Translator.providerTelegram:
value = LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI);
break;
case Translator.providerTranSmart:
value = LocaleController.getString("ProviderTranSmartTranslate", R.string.ProviderTranSmartTranslate);
break;
default:
value = "Unknown";
}

View File

@ -82,6 +82,7 @@ interface Translator {
const val providerYouDao = 6
const val providerDeepL = 7
const val providerTelegram = 8
const val providerTranSmart = 9
@Throws(Exception::class)
suspend fun translate(to: Locale, query: String): String {
@ -119,6 +120,7 @@ interface Translator {
providerYouDao -> YouDaoTranslator
providerDeepL -> DeepLTranslator
providerTelegram -> TelegramAPITranslator
providerTranSmart -> TranSmartTranslator
else -> throw IllegalArgumentException()
}

View File

@ -0,0 +1,83 @@
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 TranSmartTranslator : Translator {
private val targetLanguages = listOf(
"ar", "fr", "fil", "lo", "ja", "it", "hi", "id", "vi", "de", "km", "ms", "th", "tr", "zh", "ru", "ko", "pt", "es"
)
private fun getRandomBrowserVersion(): String {
val majorVersion = (Math.random() * 17).toInt() + 100
val minorVersion = (Math.random() * 20).toInt()
val patchVersion = (Math.random() * 20).toInt()
return "$majorVersion.$minorVersion.$patchVersion"
}
private fun getRandomOperatingSystem(): String {
val operatingSystems = arrayOf("Mac OS", "Windows")
val randomIndex = (Math.random() * operatingSystems.size).toInt()
return operatingSystems[randomIndex]
}
override suspend fun doTranslate(from: String, to: String, query: String): String {
if (to !in targetLanguages) {
error(LocaleController.getString("TranslateApiUnsupported", R.string.TranslateApiUnsupported))
}
val source = JSONArray()
for (s in query.split("\n")) {
source.put(s)
}
val response = HttpUtil.createPost("https://transmart.qq.com/api/imt")
.header("Content-Type", "application/json; charset=UTF-8")
.applyUserAgent()
.body(JSONObject().apply {
put("header", JSONObject().apply{
put("client_key", "browser-chrome-${getRandomBrowserVersion()}-${getRandomOperatingSystem()}-${UUID.randomUUID()}-${Date().time}")
put("fn", "auto_translation")
put("session", "")
put("user", "")
})
put("source", JSONObject().apply{
put("lang", if (targetLanguages.contains(from)) from else "en")
put("text_list", source)
})
put("target", JSONObject().apply{
put("lang", to)
})
put("model_category", "normal")
put("text_domain", "")
put("type", "plain")
}.toString())
.execute()
if (response.status != 200) {
error("HTTP ${response.status} : ${response.body()}")
}
val target: JSONArray = JSONObject(response.body()).getJSONArray("auto_translation")
val result = StringBuilder()
for (i in 0 until target.length()) {
result.append(target.getString(i))
if (i != target.length() - 1) {
result.append("\n")
}
}
return result.toString().trimEnd()
}
}

View File

@ -99,4 +99,5 @@
<string name="ExternalStickerCacheHint">用于与其他应用共享贴纸包 (点击选择存储位置)</string>
<string name="ExternalStickerCacheRefresh">刷新外部缓存</string>
<string name="ExternalStickerCacheDelete">删除外部缓存</string>
<string name="ProviderTranSmartTranslate">腾讯交互翻译</string>
</resources>

View File

@ -78,4 +78,5 @@
<string name="TypeMessageHintUseGroupName">使用群組名稱作為輸入框提示</string>
<string name="HideBotButtonInInputField">隱藏輸入框中的機器人按鈕</string>
<string name="ShowSendAsUnderMessageHint">在訊息提示下顯示傳送者</string>
<string name="ProviderTranSmartTranslate">騰訊交互翻譯</string>
</resources>

View File

@ -102,4 +102,5 @@
<string name="ExternalStickerCacheHint">For sharing sticker packs with other apps. Click to pick storage location.</string>
<string name="ExternalStickerCacheRefresh">Refresh External Cache</string>
<string name="ExternalStickerCacheDelete">Delete External Cache</string>
<string name="ProviderTranSmartTranslate">TranSmart Translator</string>
</resources>