Django translation.activate not working in middleware

45 Views Asked by At

I'm attempting to implement an auto language switcher in Django based on the client's IP address. For testing purposes, I've created a middleware to set the language to French ('fr').

class SetFrenchLanguageMiddleware:
def __init__(self, get_response):
    self.get_response = get_response

def __call__(self, request):
    user_language = 'fr'
    translation.activate(user_language)
    request.LANGUAGE_CODE = translation.get_language()

    response = self.get_response(request)

    response.set_cookie('django_language', user_language)
    print("language set to ", user_language)
    return response

When I access a page, I see "language set to fr" printed in the console, indicating that the middleware is executed. However:

The content is still displayed in English, the default language. The URL prefix doesn't change to "/fr/". Could someone help me understand why this isn't working as expected and guide me on how to resolve the issue?

0

There are 0 best solutions below