N
N
Nodir Malikov2021-11-22 15:35:41
Python
Nodir Malikov, 2021-11-22 15:35:41

How can I translate only the text in a CKEditor field?

In the admin panel of the Django site, I created a button that, when pressed, from one field (RichTextUploadingField) you need to take only the text, translate it using googletrans and paste it into another field.

The problem is that CKEditor generates not just text, but html tags (paragraphs, pictures, iframes, etc.).
If passed as html without filtering, then the classes along with the text are translated into another language.
I tried using regex to separate them, but it doesn't work.

How to make the tag names remain the same and only the text is translated?

UPD
Example HTML:

<p>Hello!</p>
<p><img alt="" height="360" src="/media/upl4/2021/10/19/6tuja_znjxqz2iqurlhhedcgkawy_kuk_medium.jpg" width="536" /></p>

I get:
<p>Привет!</p>
<p><Img ширина="" высота="360" src="/ media/ upl4/ 2021/ 10/ 19/ 6tuja_znjxqz2iqurlhhedcgkawy_kuk_medium.jpg" ширина="536" /></p>


Python code:
from googletrans import Translator
def translate(text, source="en", destination="ru"):
    translator = Translator()
    return translator.translate(text=text, src=source, dest=destination).text


PS data from the site is sent with ajax request

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-22
@Fayo

I see two options:
1. Getting text without tags can be done in JavaScript code. As far as I remember, there was such an opportunity in the CKEditor API.
2. Cleaning text from tags can be done on the back using https://pypi.org/project/bleach/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question