[Django]-Django: Translating a string without using language session/cookie

5👍

Looking through django/middleware/locale.py, it seems like you can just call:

from django.utils import translation
translation.activate(language)

Here’s the entire LocaleMiddleware class, for your edification:

def process_request(self, request):
    language = translation.get_language_from_request(request)
    translation.activate(language)
    request.LANGUAGE_CODE = translation.get_language()

def process_response(self, request, response):
    patch_vary_headers(response, ('Accept-Language',))
    if 'Content-Language' not in response:
        response['Content-Language'] = translation.get_language()
    translation.deactivate()
    return response

Leave a comment