Django i18n:如何不翻译管理站点?

发布于 2024-08-27 02:41:55 字数 60 浏览 5 评论 0原文

我有一个多种语言的应用程序,但我想保持管理网站始终为英语。 最好的方法是什么?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱咩 2024-09-03 02:41:55

考虑使用覆盖某些 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+)。

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.

莫言歌 2024-09-03 02:41:55

我将设置两个设置文件:

  1. settings.py 用于整个项目
  2. admin_settings.py 仅用于管理员

然后将该项目托管在不同的域中:

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

如果您有单独的管理设置文件和项目的其余部分,您可以覆盖 admin_settings.py 中的语言设置,

您可能会有非常相似的设置设置文件,因此 admin_settings.py 顶部的以下行会很方便:

from my_project.settings import *

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 *
绿萝 2024-09-03 02:41:55

这是一个有趣的问题,我找不到一个简单的直接答案,所以看起来它需要一个开箱即用的解决方案。这里有两个想法。

  1. 这可能是一种粗略的方法,但是您是否尝试删除 django.contrib.admin.locale 下除 en 之外的所有语言包?我自己没有尝试过,但我认为如果这是唯一要显示的语言环境,django 将默认返回英语。如果找不到它,它可能最终会使用基本的 django 语言环境文件,但值得一试。

  2. 我能想到的唯一其他选择是将管理主页更改为自定义视图,在该视图中手动将会话或 cookie 中的 django_language 变量设置为英语,然后重定向到正常的管理页面。

请参阅这些链接以获取一些想法。

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

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

深海夜未眠 2024-09-03 02:41:55

这是一个对我有用的简单解决方案。
只需将请求中的语言 cookie 设置为英语,并在 settings.py 中的 LocaleMiddleware 之前添加该中间件即可。
好处是无需激活和停用语言,因此无需担心会影响其他请求

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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文