Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force the Django admin to display in English

My website is not in English, but I want the Django admin to be shown only in English.

How can I force the Django admin to display in English regardless of LANGUAGE_CODE settings?

like image 613
ppp0h Avatar asked Mar 13 '23 12:03

ppp0h


2 Answers

As far as I know there is no out of the box settings for this functionality, but you can easily code it in a middleware. Add this code to a middleware.py file to one of your apps:

from django.utils import translation

class AdminLocaleMiddleware(object):

    def process_request(self, request):
        if request.path.startswith('/admin'):
            translation.activate("en")
            request.LANGUAGE_CODE = translation.get_language()

Add AdminLocaleMiddleware to your settings after LocaleMiddleware:

MIDDLEWARE_CLASSES = [
    ...
    "django.middleware.locale.LocaleMiddleware",
    "your_app.middleware.AdminLocaleMiddleware",
    ...
]
like image 149
ozren1983 Avatar answered Mar 28 '23 07:03

ozren1983


For Django 2.0 :

from django.utils import translation


class AdminLocaleMiddleware:

    def __init__(self, process_request):
        self.process_request = process_request

    def __call__(self, request):

        if request.path.startswith('/admin'):
            translation.activate("en")
            request.LANGUAGE_CODE = translation.get_language()

        response = self.process_request(request)

        return response
like image 40
Alexander Budanov Avatar answered Mar 28 '23 08:03

Alexander Budanov