在setting.py中设置:
#LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-hans' #翻译文件所在目录,需要手工创建 LOCALE_PATHS = ( os.path.join(BASE_DIR, 'locale'), ) TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_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
运行后中英文切换