Django i18n: how to not translate the admin site?

前端 未结 4 1772
情书的邮戳
情书的邮戳 2020-12-30 03:13

I have an application in several languages but I would like to keepthe admin site always in english. What is the best way to do this?

Thanks in advance.

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 03:17

    this is a simple solution that worked for me.
    just set the language cookie in the request to English, and add that middleware in settings.py before LocaleMiddleware.
    the upside is that there is to no need to activate and deactivate the language, so no need to worry that is will effect other requests

    from django.conf import settings
    from django.http import HttpRequest
    from django.utils.deprecation import MiddlewareMixin
    
    
    class ForceInEnglish(MiddlewareMixin):
        def process_request(self, request: HttpRequest) -> None:
            if request.path.startswith("/admin"):
                request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "en"
    

提交回复
热议问题