File size: 1,244 Bytes
21bc372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from gpytranslate import SyncTranslator
from pyrogram import *
from pyrogram.types import *
from config import *

trans = SyncTranslator()

@Client.on_message(
    ~filters.scheduled
    & filters.command(["tr"])
    & ~filters.forwarded
)
async def translate(_, message: Message):
    global to_translate
    reply_msg = message.reply_to_message
    if not reply_msg:
        await message.reply_text("Reply to a message to translate it!")
        return
    if reply_msg.caption:
        to_translate = reply_msg.caption
    elif reply_msg.text:
        to_translate = reply_msg.text
    try:
        args = message.text.split()[1].lower()
        if "//" in args:
            source = args.split("//")[0]
            dest = args.split("//")[1]
        else:
            source = trans.detect(to_translate)
            dest = args
    except IndexError:
        source = trans.detect(to_translate)
        dest = "en"
    translation = trans(to_translate, sourcelang=source, targetlang=dest)
    reply = ""
    reply += f"<b>Translated from {source} to {dest}</b>:\n"
    reply += f"<code>{translation.text}</code>\n"
    try:
        await message.reply_text(reply)
    except Exception as e:
        await message.reply_text(f"Error : {e}")