我有几种语言的应用程序,但我想使管理站点始终保持英语。
做这个的最好方式是什么?
提前致谢。
最佳答案
考虑使用替代某些URL的语言环境的中间件。这是一个粗糙的例子:
Django 1.9及更早版本:
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)
这只是实现的想法。
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)
如何申请?
保存到“ middleware.py”,并在设置文件中包括MIDDLEWARE_CLASSES(1.9和更早版本)或MIDDLEWARE(1.10+)。
关于django - Django i18n:如何不翻译管理网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546569/