from django.shortcuts import HttpResponse, HttpResponseRedirect
from django.utils import translation
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
from django.utils.regex_helper import re
from .forms import MainSearchForm
from .views import MainSearch


class LangBasedOnUrlMiddleware(MiddlewareMixin):
    """
    used for switching the language
    """
    @staticmethod
    def process_request(request):

        if hasattr(request, 'session'):
            active_session_lang = request.session.get(translation.LANGUAGE_SESSION_KEY)

            if active_session_lang == request.LANGUAGE_CODE:
                return

            if any(request.LANGUAGE_CODE in language for language in settings.LANGUAGES):
                translation.activate(request.LANGUAGE_CODE)
                request.session[translation.LANGUAGE_SESSION_KEY] = request.LANGUAGE_CODE
