mirror of
https://github.com/NekoX-Dev/NekoX.git
synced 2024-11-22 11:55:31 +01:00
feat: use Telegram API as a translator
This commit is contained in:
parent
733feb011e
commit
cf7001b2fc
@ -24,6 +24,10 @@ public class ConfigCellCustom extends AbstractConfigCell {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder) {
|
||||
// Not Used
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class ConfigCellTextCheck extends AbstractConfigCell {
|
||||
private final String title;
|
||||
private final String subtitle;
|
||||
private boolean enabled = true;
|
||||
public TextCheckCell cell; //TODO getCell() in NekomuraTGCell
|
||||
public TextCheckCell cell;
|
||||
|
||||
public ConfigCellTextCheck(ConfigItem bind) {
|
||||
this.bindConfig = bind;
|
||||
|
@ -318,7 +318,8 @@ public class NekoGeneralSettingsActivity extends BaseFragment {
|
||||
LocaleController.getString("ProviderLingocloud", R.string.ProviderLingocloud),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderMicrosoftTranslator),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderYouDao),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate)
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate),
|
||||
LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI)
|
||||
}, (i, __) -> {
|
||||
boolean needReset = NekoConfig.translationProvider.Int() - 1 != i && (NekoConfig.translationProvider.Int() == 1 || i == 0);
|
||||
NekoConfig.translationProvider.setConfigInt(i + 1);
|
||||
@ -410,6 +411,17 @@ public class NekoGeneralSettingsActivity extends BaseFragment {
|
||||
boolean enabled = (Boolean) newValue;
|
||||
((ConfigCellTextCheck) mapDriftingFixForGoogleMapsRow).setEnabled(!enabled);
|
||||
listAdapter.notifyItemChanged(cellGroup.rows.indexOf(mapDriftingFixForGoogleMapsRow));
|
||||
} else if (key.equals(NekoConfig.useTelegramTranslateInChat.getKey())) {
|
||||
var cell = (TextSettingsCell) (listView.findViewHolderForAdapterPosition(cellGroup.rows.indexOf(translationProviderRow)).itemView);
|
||||
if (NekoConfig.useTelegramTranslateInChat.Bool()) {
|
||||
NekoConfig.translationProvider.setConfigInt(Translator.providerTelegram);
|
||||
((ConfigCellCustom) translationProviderRow).setEnabled(false);
|
||||
cell.setEnabled(false);
|
||||
} else {
|
||||
((ConfigCellCustom) translationProviderRow).setEnabled(true);
|
||||
cell.setEnabled(true);
|
||||
}
|
||||
listAdapter.notifyItemChanged(cellGroup.rows.indexOf(translationProviderRow));
|
||||
}
|
||||
};
|
||||
|
||||
@ -684,31 +696,35 @@ public class NekoGeneralSettingsActivity extends BaseFragment {
|
||||
if (position == cellGroup.rows.indexOf(translationProviderRow)) {
|
||||
String value;
|
||||
switch (NekoConfig.translationProvider.Int()) {
|
||||
case 1:
|
||||
case Translator.providerGoogle:
|
||||
value = LocaleController.getString("ProviderGoogleTranslate", R.string.ProviderGoogleTranslate);
|
||||
break;
|
||||
case 2:
|
||||
case Translator.providerGoogleCN:
|
||||
value = LocaleController.getString("ProviderGoogleTranslateCN", R.string.ProviderGoogleTranslateCN);
|
||||
break;
|
||||
case 3:
|
||||
case Translator.providerYandex:
|
||||
value = LocaleController.getString("ProviderYandexTranslate", R.string.ProviderYandexTranslate);
|
||||
break;
|
||||
case 4:
|
||||
case Translator.providerLingo:
|
||||
value = LocaleController.getString("ProviderLingocloud", R.string.ProviderLingocloud);
|
||||
break;
|
||||
case 5:
|
||||
case Translator.providerMicrosoft:
|
||||
value = LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderMicrosoftTranslator);
|
||||
break;
|
||||
case 6:
|
||||
case Translator.providerYouDao:
|
||||
value = LocaleController.getString("ProviderYouDao", R.string.ProviderYouDao);
|
||||
break;
|
||||
case 7:
|
||||
case Translator.providerDeepL:
|
||||
value = LocaleController.getString("ProviderDeepLTranslate", R.string.ProviderDeepLTranslate);
|
||||
break;
|
||||
case Translator.providerTelegram:
|
||||
value = LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI);
|
||||
break;
|
||||
default:
|
||||
value = "Unknown";
|
||||
}
|
||||
textCell.setTextAndValue(LocaleController.getString("TranslationProvider", R.string.TranslationProvider), value, true);
|
||||
if (NekoConfig.useTelegramTranslateInChat.Bool()) textCell.setEnabled(false);
|
||||
} else if (position == cellGroup.rows.indexOf(pgpAppRow)) {
|
||||
textCell.setTextAndValue(LocaleController.getString("OpenPGPApp", R.string.OpenPGPApp), NekoXConfig.getOpenPGPAppName(), true);
|
||||
} else if (position == cellGroup.rows.indexOf(translateToLangRow)) {
|
||||
@ -781,6 +797,9 @@ public class NekoGeneralSettingsActivity extends BaseFragment {
|
||||
((ConfigCellTextCheck) mapDriftingFixForGoogleMapsRow).setEnabled(false);
|
||||
}
|
||||
|
||||
if (NekoConfig.useTelegramTranslateInChat.Bool())
|
||||
((ConfigCellCustom) translationProviderRow).setEnabled(false);
|
||||
|
||||
boolean enabled;
|
||||
|
||||
enabled = NekoConfig.largeAvatarInDrawer.Int() > 0;
|
||||
|
@ -81,6 +81,7 @@ interface Translator {
|
||||
const val providerMicrosoft = 5
|
||||
const val providerYouDao = 6
|
||||
const val providerDeepL = 7
|
||||
const val providerTelegram = 8
|
||||
|
||||
@Throws(Exception::class)
|
||||
suspend fun translate(to: Locale, query: String): String {
|
||||
@ -116,6 +117,7 @@ interface Translator {
|
||||
providerMicrosoft -> MicrosoftTranslator
|
||||
providerYouDao -> YouDaoTranslator
|
||||
providerDeepL -> DeepLTranslator
|
||||
providerTelegram -> TelegramAPITranslator
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,51 @@
|
||||
package tw.nekomimi.nekogram.transtale.source
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.InternalCoroutinesApi
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.telegram.messenger.FileLog
|
||||
import org.telegram.messenger.SharedConfig
|
||||
import org.telegram.messenger.UserConfig
|
||||
import org.telegram.tgnet.ConnectionsManager
|
||||
import org.telegram.tgnet.TLObject
|
||||
import org.telegram.tgnet.TLRPC.TL_error
|
||||
import org.telegram.tgnet.TLRPC.TL_messages_translateResultText
|
||||
import org.telegram.tgnet.TLRPC.TL_messages_translateText
|
||||
import tw.nekomimi.nekogram.transtale.Translator
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
|
||||
object TelegramAPITranslator : Translator {
|
||||
|
||||
// val targetLanguages = listOf("DE", "EN", "ES", "FR", "IT", "JA", "NL", "PL", "PT", "RU", "ZH")
|
||||
|
||||
@OptIn(InternalCoroutinesApi::class)
|
||||
override suspend fun doTranslate(from: String, to: String, query: String): String {
|
||||
|
||||
return suspendCoroutine {
|
||||
val req = TL_messages_translateText()
|
||||
req.peer = null
|
||||
req.msg_id = 0
|
||||
req.flags = req.flags or 2
|
||||
req.text = query
|
||||
req.from_lang = null
|
||||
req.to_lang = to
|
||||
|
||||
try {
|
||||
ConnectionsManager.getInstance(UserConfig.selectedAccount).sendRequest(req) { res: TLObject?, err: TL_error? ->
|
||||
if (res is TL_messages_translateResultText) {
|
||||
it.resume(res.text)
|
||||
} else {
|
||||
FileLog.e(err?.text)
|
||||
it.resumeWithException(RuntimeException("Failed to translate by Telegram API"))
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
FileLog.e(e)
|
||||
it.resumeWithException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -245,7 +245,8 @@ object AlertUtil {
|
||||
LocaleController.getString("ProviderLingocloud", R.string.ProviderLingocloud),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderMicrosoftTranslator),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderYouDao),
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate)
|
||||
LocaleController.getString("ProviderMicrosoftTranslator", R.string.ProviderDeepLTranslate),
|
||||
LocaleController.getString("ProviderTelegramAPI", R.string.ProviderTelegramAPI)
|
||||
))
|
||||
|
||||
popup.setItems(items.toTypedArray()) { item, _ ->
|
||||
|
@ -149,5 +149,5 @@
|
||||
<string name="enhancedFileLoader">加速下载</string>
|
||||
<string name="useOSMDroidMap">使用 OSMDroid 地图</string>
|
||||
<string name="mapDriftingFixForGoogleMaps">修复 Google 地图在中国的漂移问题</string>
|
||||
<string name="useTelegramTranslateInChat">在聊天中使用 Telegram 官方翻译</string>
|
||||
<string name="useTelegramTranslateInChat">在聊天中使用 Telegram 翻译界面</string>
|
||||
</resources>
|
@ -147,5 +147,6 @@
|
||||
<string name="enhancedFileLoader">Enhance Download speed</string>
|
||||
<string name="mapDriftingFixForGoogleMaps">Fix Google Maps drifting in China</string>
|
||||
<string name="useOSMDroidMap">Use OSMDroid Map</string>
|
||||
<string name="useTelegramTranslateInChat">Use Telegram Translate in Chat</string>
|
||||
<string name="useTelegramTranslateInChat">Use Telegram Translate UI in Chat</string>
|
||||
<string name="ProviderTelegramAPI">Telegram API</string>
|
||||
</resources>
|
Loading…
Reference in New Issue
Block a user