Skip to main content

LocaleMiddleware

Parse a request and decide what translation object to install in the current thread context. This allows pages to be dynamically translated to the language the user desires (if the language is available).

Attributes

AttributeTypeDescription
response_redirect_classclass = HttpResponseRedirectThe HTTP redirect class used to redirect users to language-specific URLs when a language prefix is missing from the path.

Constructor

Signature

def LocaleMiddleware(
get_response: callable = None
) - > None

Parameters

NameTypeDescription
get_responsecallable = NoneThe next middleware or view in the chain, typically passed by the Django middleware engine.

Methods


process_request()

@classmethod
def process_request(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > null

Parses the incoming request to determine the appropriate language and activates the corresponding translation object in the current thread context.

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request object containing metadata used to resolve the user's preferred language.

Returns

TypeDescription
nullNothing is returned; the method modifies the request object and global translation state in-place.

process_response()

@classmethod
def process_response(
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest),
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
) - > [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Finalizes the response by setting the Content-Language header and potentially redirecting the user to a language-prefixed URL if the original path was not found.

Parameters

NameTypeDescription
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest)The HTTP request object used to check path information and URL configuration.
response[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The outgoing HTTP response object that will be modified with language headers.

Returns

TypeDescription
[HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The original response object, or a redirect response if a language-prefixed version of the requested path is valid.