feat(discord): proteger menciones y mejorar traducción HTML para mensajes multilínea
This commit is contained in:
@@ -77,17 +77,21 @@ async def on_message(message):
|
|||||||
if not active_langs:
|
if not active_langs:
|
||||||
return
|
return
|
||||||
|
|
||||||
text_to_translate = message.content
|
import html
|
||||||
|
text_escaped = html.escape(message.content)
|
||||||
|
|
||||||
mention_pattern = re.compile(r'<@!?(\d+)>|<@&(\d+)>|<#(\d+)>')
|
# Buscamos menciones que ya están escapadas (<@...>)
|
||||||
|
mention_pattern = re.compile(r'<@!?(\d+)>|<@&(\d+)>|<#(\d+)>')
|
||||||
mentions_map = {}
|
mentions_map = {}
|
||||||
|
|
||||||
def replace_mention(match):
|
def replace_mention(match):
|
||||||
placeholder = f"__MENTION_{len(mentions_map)}__"
|
# Usamos una etiqueta autocerrada para que el traductor no intente cerrarla él mismo
|
||||||
mentions_map[placeholder] = match.group(0)
|
placeholder = f"<m{len(mentions_map)} />"
|
||||||
|
# Guardamos la mención original (sin escapar) para restaurarla luego
|
||||||
|
mentions_map[placeholder] = html.unescape(match.group(0))
|
||||||
return placeholder
|
return placeholder
|
||||||
|
|
||||||
text_to_translate = mention_pattern.sub(replace_mention, text_to_translate)
|
text_to_translate = mention_pattern.sub(replace_mention, text_escaped)
|
||||||
|
|
||||||
langs_to_show = active_langs
|
langs_to_show = active_langs
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ async def translate_text(text: str, target_lang: str) -> str:
|
|||||||
"q": text,
|
"q": text,
|
||||||
"source": "auto",
|
"source": "auto",
|
||||||
"target": target_code,
|
"target": target_code,
|
||||||
"format": "text"
|
"format": "html"
|
||||||
}
|
}
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
|||||||
@@ -34,9 +34,27 @@ class TranslationButton(discord.ui.Button):
|
|||||||
# Traducimos el texto
|
# Traducimos el texto
|
||||||
translated = await translate_text(self.text, self.lang_code)
|
translated = await translate_text(self.text, self.lang_code)
|
||||||
|
|
||||||
|
# Desescapamos el HTML para recuperar caracteres especiales
|
||||||
|
import html
|
||||||
|
import re
|
||||||
|
translated = html.unescape(translated)
|
||||||
|
|
||||||
# Reemplazamos menciones si existen
|
# Reemplazamos menciones si existen
|
||||||
for placeholder, mention in self.mentions_map.items():
|
for placeholder, mention in self.mentions_map.items():
|
||||||
translated = translated.replace(placeholder, mention)
|
# Extraer el número del tag, ej: m0 de <m0 />
|
||||||
|
match = re.search(r'm\d+', placeholder)
|
||||||
|
if not match: continue
|
||||||
|
tag_num = match.group()
|
||||||
|
|
||||||
|
# 1. Reemplazamos la etiqueta de apertura (o autocontenida) por la mención
|
||||||
|
# Patrón: < \s* mX \s* /? \s* >
|
||||||
|
open_pattern = re.compile(rf'<\s*{tag_num}\s*/?\s*>')
|
||||||
|
translated = open_pattern.sub(mention, translated)
|
||||||
|
|
||||||
|
# 2. Eliminamos cualquier etiqueta de cierre que el traductor haya "inventado"
|
||||||
|
# Patrón: < \s* / \s* mX \s* >
|
||||||
|
close_pattern = re.compile(rf'<\s*/\s*{tag_num}\s*>')
|
||||||
|
translated = close_pattern.sub('', translated)
|
||||||
|
|
||||||
# En lugar de enviar un mensaje nuevo, EDITAMOS el mensaje actual (el de los botones)
|
# En lugar de enviar un mensaje nuevo, EDITAMOS el mensaje actual (el de los botones)
|
||||||
if self.attachments:
|
if self.attachments:
|
||||||
|
|||||||
Reference in New Issue
Block a user