Skip to content

Commit

Permalink
feat: add TranSmartTranslator
Browse files Browse the repository at this point in the history
Co-authored-by: Naomi <[email protected]>
Co-authored-by: qwq233 <[email protected]>
  • Loading branch information
3 people authored and LiuYi0526 committed Jan 30, 2024
1 parent a4ff759 commit 43353aa
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ public void onItemClick(int id) {
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);
Expand Down Expand Up @@ -765,6 +766,9 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -119,6 +120,7 @@ interface Translator {
providerYouDao -> YouDaoTranslator
providerDeepL -> DeepLTranslator
providerTelegram -> TelegramAPITranslator
providerTranSmart -> TranSmartTranslator
else -> throw IllegalArgumentException()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()

}

}
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 @@ -99,4 +99,5 @@
<string name="ExternalStickerCacheHint">用于与其他应用共享贴纸包 (点击选择存储位置)</string>
<string name="ExternalStickerCacheRefresh">刷新外部缓存</string>
<string name="ExternalStickerCacheDelete">删除外部缓存</string>
<string name="ProviderTranSmartTranslate">腾讯交互翻译</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 @@ -78,4 +78,5 @@
<string name="TypeMessageHintUseGroupName">使用群組名稱作為輸入框提示</string>
<string name="HideBotButtonInInputField">隱藏輸入框中的機器人按鈕</string>
<string name="ShowSendAsUnderMessageHint">在訊息提示下顯示傳送者</string>
<string name="ProviderTranSmartTranslate">騰訊交互翻譯</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 @@ -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>

0 comments on commit 43353aa

Please sign in to comment.