aboutsummaryrefslogtreecommitdiff
path: root/middleware
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-04-18 22:26:32 -0400
committerBobby <[email protected]>2024-04-18 22:26:32 -0400
commit9f46bf5238c8c177bbcf02783600975e0ef245c2 (patch)
treefc28218caf29bbc0e23adc6299b8c2f7993212a5 /middleware
parent3721a4d2ca2eca8f0d43b9650627fb74da6d1428 (diff)
downloadthatcomputerscientist-9f46bf5238c8c177bbcf02783600975e0ef245c2.tar.xz
thatcomputerscientist-9f46bf5238c8c177bbcf02783600975e0ef245c2.zip
Translated Pages are Cached
Diffstat (limited to 'middleware')
-rw-r--r--middleware/translationMiddleware.py47
1 files changed, 0 insertions, 47 deletions
diff --git a/middleware/translationMiddleware.py b/middleware/translationMiddleware.py
deleted file mode 100644
index 06373877..00000000
--- a/middleware/translationMiddleware.py
+++ /dev/null
@@ -1,47 +0,0 @@
-import os
-
-from bs4 import BeautifulSoup
-from django.conf import settings
-from django.http import HttpResponse
-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
-
-class TranslationMiddleware:
- def __init__(self, get_response):
- self.get_response = get_response
- self.CACHE_TTL = 60 * 60
-
- def __call__(self, request):
- response = self.get_response(request)
- lang_cookie = request.COOKIES.get('lang', '')
-
- if lang_cookie != 'ja':
- return response
-
- content_type = response.get('Content-Type', '').lower()
- if 'text' not in content_type:
- return response
-
- HTML_content =response.content.decode('utf-8')
-
- # 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",
- "ザットコンピュータサイエンティスト"
- )
-
- translate_client = translate.Client()
- target_language = 'ja'
- translated_content = translate_client.translate(
- HTML_content,
- target_language=target_language,
- )['translatedText']
-
- return HttpResponse(translated_content)