mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
fix(translate): make markdown render properly
This commit is contained in:
@@ -170,50 +170,70 @@ def translate_command():
|
|||||||
async def _translate_with_google_web(text: str, from_lang: str = "auto", to_lang: str = "en") -> dict:
|
async def _translate_with_google_web(text: str, from_lang: str = "auto", to_lang: str = "en") -> dict:
|
||||||
try:
|
try:
|
||||||
base_url = "https://translate.googleapis.com/translate_a/single"
|
base_url = "https://translate.googleapis.com/translate_a/single"
|
||||||
|
|
||||||
params = {
|
def chunk_text(t: str, max_len: int = 900) -> list[str]:
|
||||||
"client": "gtx",
|
t = t.strip()
|
||||||
"sl": from_lang,
|
if len(t) <= max_len:
|
||||||
"tl": to_lang,
|
return [t]
|
||||||
"dt": ["t", "bd"],
|
chunks = []
|
||||||
"q": text
|
current = []
|
||||||
}
|
current_len = 0
|
||||||
|
for part in re.split(r"(\n+|\s+)", t):
|
||||||
param_string = "&".join([f"{k}={'&'.join(v) if isinstance(v, list) else v}" for k, v in params.items()])
|
if not part:
|
||||||
url = f"{base_url}?{param_string}"
|
continue
|
||||||
|
add_len = len(part)
|
||||||
|
if current_len + add_len > max_len and current:
|
||||||
|
chunks.append("".join(current))
|
||||||
|
current = [part]
|
||||||
|
current_len = add_len
|
||||||
|
else:
|
||||||
|
current.append(part)
|
||||||
|
current_len += add_len
|
||||||
|
if current:
|
||||||
|
chunks.append("".join(current))
|
||||||
|
return chunks
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chunks = chunk_text(text)
|
||||||
|
translated_parts: list[str] = []
|
||||||
|
detected_lang_overall = from_lang
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url, headers=headers) as response:
|
for idx, chunk in enumerate(chunks):
|
||||||
if response.status == 200:
|
params = {
|
||||||
result_text = await response.text()
|
"client": "gtx",
|
||||||
|
"sl": from_lang,
|
||||||
|
"tl": to_lang,
|
||||||
|
"dt": ["t", "bd"],
|
||||||
|
"q": chunk
|
||||||
|
}
|
||||||
|
async with session.get(base_url, headers=headers, params=params) as response:
|
||||||
|
if response.status != 200:
|
||||||
|
return None
|
||||||
|
result_text = (await response.text()).strip()
|
||||||
try:
|
try:
|
||||||
result_text = result_text.strip()
|
if result_text.startswith("[["):
|
||||||
if result_text.startswith('[['):
|
|
||||||
data = json.loads(result_text)
|
data = json.loads(result_text)
|
||||||
|
part_text = ""
|
||||||
translated_text = ""
|
|
||||||
if data and len(data) > 0 and data[0]:
|
if data and len(data) > 0 and data[0]:
|
||||||
for item in data[0]:
|
for item in data[0]:
|
||||||
if item and len(item) > 0:
|
if item and len(item) > 0:
|
||||||
translated_text += item[0] if item[0] else ""
|
part_text += item[0] if item[0] else ""
|
||||||
|
translated_parts.append(part_text)
|
||||||
detected_lang = from_lang
|
if idx == 0 and len(data) > 2 and data[2]:
|
||||||
if len(data) > 2 and data[2]:
|
detected_lang_overall = data[2]
|
||||||
detected_lang = data[2]
|
else:
|
||||||
|
return None
|
||||||
return {
|
except Exception:
|
||||||
"translatedText": translated_text.strip(),
|
return None
|
||||||
"detectedSourceLanguage": detected_lang
|
|
||||||
}
|
return {
|
||||||
except:
|
"translatedText": "".join(translated_parts).strip(),
|
||||||
pass
|
"detectedSourceLanguage": detected_lang_overall
|
||||||
|
}
|
||||||
return None
|
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -280,7 +300,7 @@ def translate_command():
|
|||||||
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title="Translation",
|
title="Translation",
|
||||||
description=f"**Original:** {text}\n**Translated:** {result['translatedText']}",
|
description=f"**Original:** {text}\n\n**Translated:** {result['translatedText']}",
|
||||||
color=0x7289DA,
|
color=0x7289DA,
|
||||||
)
|
)
|
||||||
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
embed.set_author(name="Utility", icon_url="https://yes.nighty.works/raw/8VLDcg.webp")
|
||||||
|
|||||||
Reference in New Issue
Block a user