Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django i18n: how to not translate the admin site?

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.

like image 203
filias Avatar asked Mar 30 '10 15:03

filias


People also ask

What is lazy text in Django?

Use the lazy versions of translation functions in django. utils. translation (easily recognizable by the lazy suffix in their names) to translate strings lazily – when the value is accessed rather than when they're called. These functions store a lazy reference to the string – not the actual translation.

How do I change the default language in Django?

Force Django to use settings. LANGUAGE_CODE for default language instead of request. META['HTTP_ACCEPT_LANGUAGE'] · GitHub.

What is internationalization in Django?

The goal of internationalization and localization is to allow a single web application to offer its content in languages and formats tailored to the audience. Django has full support for translation of text, formatting of dates, times and numbers, and time zones.


2 Answers

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.

like image 75
Tommaso Barbugli Avatar answered Oct 20 '22 04:10

Tommaso Barbugli


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 *
like image 42
Tomasz Wysocki Avatar answered Oct 20 '22 04:10

Tomasz Wysocki