更改django-admin-tools的区域设置 [英] Change locale for django-admin-tools

查看:132
本文介绍了更改django-admin-tools的区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 settings.py 文件中我有:

LANGUAGE_CODE = 'ru-RU'

还有,我已经安装并工作了django-admin-tools。但是管理语言还是英文。我在做什么错?

also, I have installed and working django-admin-tools. But admin language still english. What I'm doing wrong?

PS。

$ cat settings.py | grep USE | grep -v USER
USE_I18N = True
USE_L10N = True
USE_TZ = True


推荐答案

您需要为管理员应用程序专门设置语言。由于django不提供语言下拉列表作为默认登录的一部分,您有几个选项:

You need to set the language specifically for the admin app. Since django does not provide a language drop down as part of the default login, you have a few options:


  1. 登录到您的正常(非管理员视图),具有超级用户/员工凭据和正确的语言,然后切换到管理URL。

  1. Login to your normal (non admin view), with superuser/staff credentials and the correct language, then shift over to the admin URL.

更新管理模板并添加语言下拉列表查看此片段

Update the admin templates and add a language dropdown see this snippet.

创建一些自定义中间件设置管理员的语言:

Create some custom middleware to set the language for admin:

from django.conf import settings
from django.utils import translation

class AdminLocaleMiddleware:

    def process_request(self, request):
        if request.path.startswith('/admin'):
            request.LANG = getattr(settings, 'ADMIN_LANGUAGE_CODE',
                                   settings.LANGUAGE_CODE)
            translation.activate(request.LANG)
            request.LANGUAGE_CODE = request.LANG

将其添加到您的 MIDDLEWARE_CLASSES

Add it to your MIDDLEWARE_CLASSES

MIDDLEWARE_CLASSES = {
    # ...
    'foo.bar.AdminLocaleMiddleware',
    # ...
}

settings.py 中设置管理员所需的语言:

Set the language you want for the admin in settings.py:

ADMIN_LANGUAGE_CODE = 'ru-RU'


这篇关于更改django-admin-tools的区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆