OSError: 找不到默认语言zh_Hans的翻译文件。

3

django: 1.10.3 本地环境:Windows 服务器环境:Debian

本地环境可以正常运行,但在我的服务器上无法运行,但可以通过en-us运行,为什么我不能通过zh_hans运行?

Traceback:

      ...File "/home/a/ENV/lib/python3.5/site-packages/django/utils/translation/__init__.py", line 85, in ugettext
        return _trans.ugettext(message)
      File "/home/a/ENV/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 337, in gettext
        return do_translate(message, 'gettext')
      File "/home/a/ENV/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 320, in do_translate
        _default = _default or translation(settings.LANGUAGE_CODE)
      File "/home/a/ENV/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 227, in translation
        _translations[language] = DjangoTranslation(language)
      File "/home/a/ENV/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 134, in __init__
        raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
    OSError: No translation files found for default language zh_Hans.

更新:settings.py

"""
Django settings for aWebsite project.

Generated by 'django-admin startproject' using Django 1.10.3.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

# 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/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'a*x^k((1zswa$g_qu70f%%8mb6z7p1tfzy%&+@kye+3zdafv3h'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['aWebsite.com']


# Application definition

INSTALLED_APPS = [
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ckeditor',
    'ckeditor_uploader',
    'imagekit',
    'Website',
]

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

ROOT_URLCONF = 'aWebsite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        '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 = 'hitekCable.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'sdefg.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/topics/i18n/

#LANGUAGE_CODE = 'zh_Hans'
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = 'http:/aWebsite.com/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')

STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'Website/static'))]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'http://aWebsite.com/media_hitek/'


CKEDITOR_UPLOAD_PATH = "uploads/"  
CKEDITOR_UPLOAD_SLUGIFY_FILENAME = True
CKEDITOR_ALLOW_NONIMAGE_FILES = False
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_IMAGE_BACKEND = 'pillow' 
CKEDITOR_JQUERY_URL = '//cdn.bootcss.com/jquery/2.2.4/jquery.min.js'
CKEDITOR_CONFIGS = {
    'default': {
        'skin': 'moono',
        # 'skin': 'office2013',
        'toolbar_Basic': [
            ['Source', '-', 'Bold', 'Italic']
        ],
        'toolbar_YourCustomToolbarConfig': [
            {'name': 'document', 'items': ['Source', '-', 'Print', '-', 'Templates']},#'Save', 'NewPage', 'Preview',
            {'name': 'clipboard', 'items': ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo']},
            {'name': 'editing', 'items': ['Find', 'Replace', '-', 'SelectAll']},
            {'name': 'forms',
             'items': ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
                       'HiddenField']},
            '/',
            {'name': 'basicstyles',
             'items': ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat']},
            {'name': 'paragraph',
             'items': ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-',
                       'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl',
                       'Language']},
            {'name': 'links', 'items': ['Link', 'Unlink', 'Anchor']},
            {'name': 'insert',
             'items': ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe']},
            # '/',
            {'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']},
            {'name': 'colors', 'items': ['TextColor', 'BGColor']},
            {'name': 'tools', 'items': ['Maximize', 'ShowBlocks']},
            # {'name': 'about', 'items': ['About']},
            # '/',  # put this to force next toolbar on new line
            # {'name': 'yourcustomtools', 'items': [
            #     # put the name of your editor.ui.addButton here
            #     'Preview',
            #     'Maximize',
            #
            # ]},
        ],
        'toolbar': 'YourCustomToolbarConfig',  # put selected toolbar config here
        # 'toolbarGroups': [{ 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ] }],
        # 'height': 291,
        # 'width': '100%',
        # 'filebrowserWindowHeight': 725,
        # 'filebrowserWindowWidth': 940,
        # 'toolbarCanCollapse': True,
        # 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML',
        'tabSpaces': 4,
        'extraPlugins': ','.join(
            [
                # your extra plugins here
                'div',
                'autolink',
                'autoembed',
                'embedsemantic',
                'autogrow',
                # 'devtools',
                'widget',
                'lineutils',
                'clipboard',
                'dialog',
                'dialogui',
                'elementspath'
            ]),
    }
}


你能分享一下你的设置文件吗? - Rajesh Yogeshwar
是的,你现在可以看到了。 - zofan
好的,如果您要将zh_Hans作为默认语言使用,则需要提供相应的翻译文件。在设置文件中,您需要设置几个变量,以便Django知道在哪里查找翻译文件以及其他一些信息。请仔细阅读文档,并按照其中提到的步骤进行操作。 - Rajesh Yogeshwar
2个回答

4

您应该使用:

LANGUAGE_CODE = 'zh-hans'

当您使用django-admin makemessages -l zh_hans时,也要注意不同的使用方式: 一个是zh-hans,另一个是zh_hans。


0
我遇到了与下面相同的错误:

OSError: 找不到默认语言 hello 的翻译文件。

当我在 settings.py 中设置 helloLANGUAGE_CODELANGUAGES,如下所示。*我正在使用Django 4.2.1学习翻译
# "settings.py"

LANGUAGE_CODE = 'hello'

from django.utils.translation import gettext_lazy as _

LANGUAGES = (
    ('hello', _('Hello')),
)

所以,我运行了下面的makemessages,但是我无法解决这个错误:

django-admin makemessages -l hello

所以,我运行了下面的compilemessages,然后就解决了错误:

django-admin compilemessages

但是,我遇到了另一个错误,而且无法通过多种解决方案来解决这个错误:

ValueError: plural form 中存在无效的标记:EXPRESSION

最终,我发现下面的答案

你应该使用在 django/conf/global_settings.py 中 LANGUAGES 列表中精确列出的 LANGUAGE_CODE

因此,我应该将 django/conf/global_settings.py 中列出的语言设置为 LANGUAGE_CODEsettings.py 中的 LANGUAGES


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接