Django i18n:如何不翻译管理站点? [英] Django i18n: how to not translate the admin site?
本文介绍了Django i18n:如何不翻译管理站点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用多种语言的应用程序,但我希望管理站点始终使用英语.最好的方法是什么?
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?
提前致谢.
推荐答案
考虑使用覆盖某些 URL 区域设置的中间件.这是一个粗略的例子:
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)
如何申请?
保存到middleware.py",并在设置文件中包含到 MIDDLEWARE_CLASSES(1.9 及更早版本)或 MIDDLEWARE (1.10+).
How to apply?
Save to 'middleware.py', and include to MIDDLEWARE_CLASSES (1.9 and earlier) or MIDDLEWARE (1.10+) in the settings file.
这篇关于Django i18n:如何不翻译管理站点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文