为什么在尝试在 django settings.py 中转换语言时会出现错误?


import os
#added to change LANGUAGES
from django.utils.translation import ugettext_lazy as _
#added to change LANGUAGES
LANGUAGES = [
('ko', _('Korean')),
('en', _('English')),
]
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '7)30po8g=7ex!bz-(&*$y84ewejhu$t4zf^$mnja&b2ibg52-&'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CheckbomOX.apps.CheckbomoxConfig'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware', #added to change LANGUAGES
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Checkbom.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Checkbom.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'ko-kr'
#added to change LANGUAGES
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Seoul'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'

这是我的 settings.py

我添加了

django.middleware.locale.LocaleMiddleware

在中间件

并且还添加了

from django.utils.translation import ugettext_lazy as _

并且还添加了

LOCALE_PATHS = (os.path.join(BASE_DIR, 'locale')

我还安装了 gettext 0.20.1。

我执行了

python manage.py makemessages -l en

python manage.py makemessages -l ko

当我运行 django 服务器时,

?:(翻译。E004( 您为"语言"设置中没有的LANGUAGE_CODE设置提供了一个值。

弹出错误。我该如何解决这个问题?我安装了 gettext 静态 0.20.1。在本地文件夹中,创建了 en 和 kr 文件夹,在这两个文件夹中,都有 django.po 和 django.mo。

错误非常明确:

?:(翻译。E004( 您为"语言"设置中没有的LANGUAGE_CODE设置提供了一个值。

LANGUAGE_CODE = 'ko-kr'

不在:

LANGUAGES = [
('ko', _('Korean')),
('en', _('English')),
]

自 Django 3.0 以来,LANGUAGE_CODE设置发生了变化:要将语言设置为韩语,您应该将LANGUAGE_CODE设置设置为"ko",而不是"ko-KR"。

参见:https://devlog.jwgo.kr/2019/12/24/django-3-language-code-error/(韩语链接(

相关内容

最新更新