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.
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.
Force Django to use settings. LANGUAGE_CODE for default language instead of request. META['HTTP_ACCEPT_LANGUAGE'] · GitHub.
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.
Consider using middleware that overrides the locale for certain URLs. Here's a rough example:
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.
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)
Save to 'middleware.py', and include to MIDDLEWARE_CLASSES (1.9 and earlier) or MIDDLEWARE (1.10+) in the settings file.
I would setup two settings files:
settings.py
for whole projectadmin_settings.py
for admin onlyThen host this project in separate domains:
example.com
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 *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With