aboutsummaryrefslogtreecommitdiff
path: root/middleware/translationMiddleware.py
blob: a67453d04eeea9633ca22454a3f86a726954e78c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
from bs4 import BeautifulSoup
from django.http import HttpResponse

from django.conf import settings
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)