In admin/middleware.py:
from django.utils import translation
class AdminUsesEnglishMiddleware:
"""
Force English to be used within admin section
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.path.startswith('/admin/'):
with translation.override('en'):
return self.get_response(request)
else:
return self.get_response(request)
Then register the middleware in settings/__init__.py:
MIDDLEWARE = (
.........<your other middleware>
'admin.middleware.AdminUsesEnglishMiddleware',
)
What’s happening? We are running this middleware before every request where we check if the path matches our admin section’s path, and then temporarily changing the language with translation.override('en')
, which is changed back to the previous language after the middleware runs.
Confirmed as working in Django 3.2, but should work in other versions of Django as well.