【问题标题】:Why is the error occur when trying to chang language in django settings.py?为什么尝试更改 django settings.py 中的语言时会出现错误?
【发布时间】:2020-05-11 08:36:00
【问题描述】:
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 服务器时,

?: (translation.E004) 您为 LANGUAGE_CODE 设置提供的值不在 LANGUAGES 设置中。

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

【问题讨论】:

    标签: python django-settings


    【解决方案1】:

    自 Django 3.0 以来 LANGUAGE_CODE 设置已更改:要将语言设置为韩语,您应该将 LANGUAGE_CODE 设置设置为“ko”,而不是“ko-KR”。

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

    【讨论】:

      【解决方案2】:

      错误非常明确:

      ?: (translation.E004) 您为 LANGUAGE_CODE 设置提供的值不在 LANGUAGES 设置中。

      LANGUAGE_CODE = 'ko-kr'
      

      不在:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        相关资源
        最近更新 更多