Answer the question
In order to leave comments, you need to log in
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>
<p>Привет!</p>
<p><Img ширина="" высота="360" src="/ media/ upl4/ 2021/ 10/ 19/ 6tuja_znjxqz2iqurlhhedcgkawy_kuk_medium.jpg" ширина="536" /></p>
from googletrans import Translator
def translate(text, source="en", destination="ru"):
translator = Translator()
return translator.translate(text=text, src=source, dest=destination).text
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question