From 5de7630148dd9ef69a66ea269e0b8077628e51ca Mon Sep 17 00:00:00 2001 From: Bobby Date: Tue, 6 Jun 2023 20:14:28 -0400 Subject: Fix code highlight for Japanese --- middleware/translationMiddleware.py | 14 +++++++++++--- middleware/uuidmiddleware.py | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'middleware') diff --git a/middleware/translationMiddleware.py b/middleware/translationMiddleware.py index de143286..ba1eda1f 100644 --- a/middleware/translationMiddleware.py +++ b/middleware/translationMiddleware.py @@ -1,7 +1,9 @@ -from google.cloud import translate_v2 as translate +import os + +from bs4 import BeautifulSoup from django.conf import settings -import os from django.core.cache import cache +from google.cloud import translate_v2 as translate cred_path = os.path.join(settings.BASE_DIR, 'credentials-translate.json') os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cred_path @@ -21,7 +23,13 @@ class TranslationMiddleware: if 'text' not in content_type: return response - HTML_content = response.content.decode('utf-8') + HTML_content =response.content.decode('utf-8').strip() + + # add no translate class to the 'highlight' class + soup = BeautifulSoup(HTML_content, 'html.parser') + for tag in soup.find_all(class_='highlight'): + tag['class'].append('notranslate') + HTML_content = str(soup) HTML_content = HTML_content.replace( "That Computer Scientist", diff --git a/middleware/uuidmiddleware.py b/middleware/uuidmiddleware.py index 859000c5..212e67b2 100644 --- a/middleware/uuidmiddleware.py +++ b/middleware/uuidmiddleware.py @@ -1,6 +1,8 @@ +import json import uuid + import redis -import json + redis_instance = redis.StrictRedis(host='localhost', port=6379, db=0) -- cgit v1.2.3