千家信息网

Django 2.2中怎么实现 i18n 多语言国际化

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,本篇文章为大家展示了Django 2.2中怎么实现 i18n 多语言国际化,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在setting.py中设置:#LAN
千家信息网最后更新 2025年02月03日Django 2.2中怎么实现 i18n 多语言国际化

本篇文章为大家展示了Django 2.2中怎么实现 i18n 多语言国际化,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

在setting.py中设置:

#LANGUAGE_CODE = 'en-us'LANGUAGE_CODE = 'zh-hans'#翻译文件所在目录,需要手工创建LOCALE_PATHS = (    os.path.join(BASE_DIR, 'locale'),)TIME_ZONE = 'Asia/Shanghai'USE_I18N = TrueUSE_L10N = True

在MIDDLEWARE 中加入'django.middleware.locale.LocaleMiddleware':

MIDDLEWARE = [    'django.middleware.security.SecurityMiddleware',    'django.contrib.sessions.middleware.SessionMiddleware',    'django.middleware.locale.LocaleMiddleware',    'django.middleware.common.CommonMiddleware',    'django.middleware.csrf.CsrfViewMiddleware',    'django.contrib.auth.middleware.AuthenticationMiddleware',    'django.contrib.messages.middleware.MessageMiddleware',    'django.middleware.clickjacking.XFrameOptionsMiddleware',]


在TEMPLATES添加'django.template.context_processors.i18n'

TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        'DIRS': [os.path.join(BASE_DIR, 'templates')],        'APP_DIRS': True,        'OPTIONS': {            'context_processors': [                'django.template.context_processors.i18n',                'django.template.context_processors.debug',                'django.template.context_processors.request',                'django.contrib.auth.context_processors.auth',                'django.contrib.messages.context_processors.messages',            ],        },    },]

mkdir -p locale

django-admin makemessages -l zh_Hans
django-admin compilemessages

或者

python3 django-admin makemessages -l zh_Hans
python3 django-admin compilemessages

上述内容就是Django 2.2中怎么实现 i18n 多语言国际化,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0