Skip to content

Commit c3378b7

Browse files
Update translator.py
Co-authored-by: Kolqvr <kolqvr@users.noreply.github.com>
1 parent 2b227ce commit c3378b7

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

utils/translator.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
1-
# TODO
2-
# WARNING! IT COMMENTED FULLY SINCE INSTALL PACKAGES FROM THIS MODULE WILL
3-
# BROKE ALL FUCKING Moon, NOT JUST THIS MODULE
4-
# THIS MODULE IS INCOMPATIBLE WITH LAST Moon VERSION
5-
# SINCE IT USE OUTDATED PACKAGE WITH BROKEN DEPENDENCIES
6-
# IT NEEDS TO BE REWRITEN
7-
8-
from utils.scripts import format_small_module_help, import_library
9-
from utils.misc import modules_help, prefix
10-
from pyrogram import Client, filters
1+
import json
112

12-
googletrans = import_library("googletrans", "googletrans-py")
13-
from googletrans import Translator
3+
import requests
4+
5+
from pyrogram import Client, filters
146

15-
trl = Translator()
7+
from utils.misc import modules_help, prefix
8+
from utils.scripts import format_small_module_help
169

1710

1811
@Client.on_message(filters.command(["trans", "tr"], prefix) & filters.me)
1912
async def translatedl(_client, message):
2013
try:
14+
# Parse command arguments
2115
if len(message.command) > 1:
22-
dtarget = message.text.split(None, 2)[1]
16+
dtarget = message.command[1]
2317
else:
2418
dtarget = "en"
19+
2520
if len(message.command) > 2:
2621
dtext = message.text.split(None, 2)[2]
2722
elif message.reply_to_message:
2823
dtext = message.reply_to_message.text
2924
else:
30-
message.edit_text(format_small_module_help("translator"))
25+
await message.edit_text(format_small_module_help("translator"))
26+
return
27+
3128
await message.edit_text("<b>Translating</b>")
32-
dtekstr = trl.translate(dtext, dest=dtarget)
29+
30+
# Use the Google Translate API endpoint
31+
url = "https://clients5.google.com/translate_a/t"
32+
params = {
33+
"client": "dict-chrome-ex",
34+
"sl": "auto",
35+
"tl": dtarget,
36+
"q": dtext
37+
}
38+
39+
response = requests.get(url, params=params)
40+
41+
if response.status_code != 200:
42+
await message.edit_text(f"<b>Error:</b> API returned status code {response.status_code}")
43+
return
44+
45+
# Parse the JSON response
46+
data = json.loads(response.text)
47+
48+
# Based on actual response format [["translated_text", "detected_language"]]
49+
if isinstance(data, list) and len(data) > 0 and isinstance(data[0], list) and len(data[0]) > 0:
50+
translated_text = data[0][0]
51+
# Check if language detection is included
52+
source_lang = data[0][1] if len(data[0]) > 1 else "auto"
53+
else:
54+
translated_text = "Translation failed"
55+
source_lang = "unknown"
56+
3357
await message.edit_text(
34-
f"<b>Translated</b> to <code>{dtarget}</code> :\n\n"
35-
+ "{}".format(dtekstr.text)
58+
f"<b>Translated</b> from <code>{
59+
source_lang}</code> to <code>{dtarget}</code>:\n\n"
60+
+ "{}".format(translated_text)
3661
)
37-
except ValueError as err:
38-
await message.edit("Error: <code>{}</code>".format(str(err)))
62+
63+
except Exception as err:
64+
await message.edit_text(f"<b>Error:</b> <code>{str(err)}</code>")
3965
return
4066

4167

4268
modules_help["translator"] = {
43-
"tr": "[lang]* [text/reply]* translate message",
44-
"trans": "[lang]* [text/reply]* translate message \n If lang not given it'll use default(en)",
69+
"tr": "[lang] [text/reply] translate message",
70+
"trans": "[lang] [text/reply] translate message \n\nIf lang not given it'll use default(en)",
4571
}

0 commit comments

Comments
 (0)