18

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.

filias
  • 244
  • 2
  • 9

4 Answers4

15

Consider using middleware that overrides the locale for certain URLs. Here's a rough example:

Django 1.9 and earlier:

from django.conf import settings    
from django.utils.translation import activate     
import re

class ForceInEnglish(object):

    def process_request(self, request):   
        if re.match(".*admin/", request.path):          
            activate("en")      
        else:
            activate(settings.LANGUAGE_CODE)

This is just an idea of implementation.

Django 1.10+:

from django.conf import settings
from django.utils import translation


class ForceInEnglish:

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

    def __call__(self, request):
        if request.path.startswith('/admin'):
            request.LANG = 'en'
            translation.activate(request.LANG)
            request.LANGUAGE_CODE = request.LANG

        return self.get_response(request)

How to apply?

Save to 'middleware.py', and include to MIDDLEWARE_CLASSES (1.9 and earlier) or MIDDLEWARE (1.10+) in the settings file.

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41
  • Is this thread safe? We've just discovered that this code was changing the non-admin into english too. – Tom Viner Aug 22 '11 at 09:39
  • Turns out the problem is caused by `activate(get_language())`, which merely activates the previously activated language (ie `en`). Using `activate(settings.LANGUAGE_CODE)` fixed this for me. – Tom Viner Aug 23 '11 at 11:49
  • I think using activate(request.LANGUAGE_CODE) instead of settings.LANGUAGE_CODE will be a bit better. Don't you think so? @Tommaso Barbugli – Alexey Sidash Feb 04 '14 at 16:02
  • Be careful with the regex matching, this will match any url that contains `.*admin/` in any part of the url, even if the page does not belong to the admin site. It would be better to start the regex with the `^` character, or use Python's `startswith`. – thnee Apr 14 '15 at 11:59
4

I would setup two settings files:

  1. settings.py for whole project
  2. admin_settings.py for admin only

Then host this project in separate domains:

  1. example.com
  2. admin.example.com

If you have separate settings files for admin and rest of the project, you can override language settings in your admin_settings.py

You will probably have very similar settings files, so following line on the top of admin_settings.py will be handy:

from my_project.settings import *
Tomasz Wysocki
  • 11,170
  • 6
  • 47
  • 62
  • 1
    How would you configure the admin site to actually use the admin_settings.py file? – thnee Mar 31 '13 at 16:17
  • @thnee You can choose the settings file in `manage.py` and `wsgi.py` (change both). – Mark Mar 09 '15 at 11:02
  • @Mark That would not affect just the admin site, but all parts of the whole project. – thnee Apr 14 '15 at 11:54
  • @thnee The two domains would have their own processes with their own wsgi.py and manage.py (and settings.py). They can share the rest of the code and the database(s). You can choose seprate SESSION_COOKIE_DOMAIN values if you want to seperate the admin sessions from the main ones. Or you can share them, in which case SECRET_KEY should also be the same. – Mark Apr 15 '15 at 08:41
0

This is an interesting problem, and I couldn't find an easy strait forward answer so it looks like it will require an out of the box solution. Here are two ideas.

  1. This might be a crude way of doing it, but did you try deleting all of the language bundles under django.contrib.admin.locale except for en? I haven't tried it myself, but I think django will default back to english if that is the only locale left to display. It may just end up using the base django locale files if it can't find it but it is worth a try.

  2. The only other option that I could think of was to change the admin home page to a custom view where you manually set the django_language variable in the session or cookie to english and then redirect to the normal admin page.

See these links for some ideas.

http://code.djangoproject.com/browser/django/trunk/django/views/i18n.py

http://docs.djangoproject.com/en/1.2/topics/i18n/internationalization/#the-set-language-redirect-view

Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • For right-to-left languages, method 1 might cause the admin to be displayed in English but right-to-left. – Flimm Dec 23 '15 at 14:32
0

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"
lev
  • 3,986
  • 4
  • 33
  • 46