Django 翻译的另一个问题
Yet another issue with Django Translation
在阅读了数十个关于翻译不工作的问答后,我放弃了,因为我做了(我想我做了)其他线程中提到的所有内容,但仍然无法完成工作。
我的settings.py:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from socket import gethostname
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
HOSTNAME = gethostname()
def make_path(path):
return os.path.join(BASE_DIR, path)
def make_abs_path(path):
return os.path.abspath(make_path(path))
def get_proj_root():
curr_path = make_abs_path('')
return os.path.split(curr_path)[0]
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = xxxxx
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
ADMINS = ()
MANAGERS = ADMINS
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'rest_framework',
# 'rest_framework.authtoken',
'main'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'Plan_Zajec.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'main.context.header_context',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
ROOT_URLCONF = 'Plan_Zajec.urls'
WSGI_APPLICATION = 'Plan_Zajec.wsgi.application'
DATABASES = {
'default': {xxxxx}
}
DEFAULT_CHARSET = 'utf-8'
TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = make_path('static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
make_path('main/static/main'),
)
LANGUAGES = (
('pl', 'Polski'),
('en', 'English'),
)
LANGUAGE_CODE = 'pl'
LOCALE_PATH = make_abs_path('locale/')
LOGIN_URL = '/'
# Celery conf:
BROKER_URL = 'redis://'
CELERY_RESULT_BACKEND = 'redis://'
CELERY_MAX_CACHED_RESULTS = 1000
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
urls.py,在 urlspatterns 的末尾:
url(r'^i18n/', include('django.conf.urls.i18n')),
页眉中的表格(稍后将替换为 nice 'click-on-flag' select,但现在...)
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Zmień język" />
</form>
简单地说 - 我已经完成了 docs 所说的一切。 Django 保存并记住 LANGUAGE_CODE (我可以看到,因为 'selected' if 在形式中,我仔细检查了它),但无论如何都无法翻译。
LOCALE_PATH有效:
>>> settings.LOCALE_PATH
'/home/mefioo/apps/generator/src/locale/'
我已经 运行 'compilemessages':
locale/
└── en
└── LC_MESSAGES
├── django.mo
└── django.po
有什么想法吗?
编辑:在 MIDDLEWARE_CLASSES 中排序 - 在 session 之后,在通用之前,仍然无法正常工作。
编辑:根据@Andrea Corbellini 的建议,我也为'pl' 做了翻译,msgstr
等于msgid
,仍然没有。我如何检查 Django 是否找到语言环境文件,即使我确定 LOCALE_PATH 是正确的?
好吧,我瞎了,我的大脑欺骗了我,没有 LOCALE_PATH
这样的东西,有 LOCALE_PATHS
。我添加了这个小 'S',现在一切正常。 (我要回小学再学读书,有问题请见谅。)
在阅读了数十个关于翻译不工作的问答后,我放弃了,因为我做了(我想我做了)其他线程中提到的所有内容,但仍然无法完成工作。
我的settings.py:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from socket import gethostname
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
HOSTNAME = gethostname()
def make_path(path):
return os.path.join(BASE_DIR, path)
def make_abs_path(path):
return os.path.abspath(make_path(path))
def get_proj_root():
curr_path = make_abs_path('')
return os.path.split(curr_path)[0]
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = xxxxx
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
ADMINS = ()
MANAGERS = ADMINS
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'rest_framework',
# 'rest_framework.authtoken',
'main'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'Plan_Zajec.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'main.context.header_context',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
ROOT_URLCONF = 'Plan_Zajec.urls'
WSGI_APPLICATION = 'Plan_Zajec.wsgi.application'
DATABASES = {
'default': {xxxxx}
}
DEFAULT_CHARSET = 'utf-8'
TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = make_path('static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
make_path('main/static/main'),
)
LANGUAGES = (
('pl', 'Polski'),
('en', 'English'),
)
LANGUAGE_CODE = 'pl'
LOCALE_PATH = make_abs_path('locale/')
LOGIN_URL = '/'
# Celery conf:
BROKER_URL = 'redis://'
CELERY_RESULT_BACKEND = 'redis://'
CELERY_MAX_CACHED_RESULTS = 1000
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
urls.py,在 urlspatterns 的末尾:
url(r'^i18n/', include('django.conf.urls.i18n')),
页眉中的表格(稍后将替换为 nice 'click-on-flag' select,但现在...)
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Zmień język" />
</form>
简单地说 - 我已经完成了 docs 所说的一切。 Django 保存并记住 LANGUAGE_CODE (我可以看到,因为 'selected' if 在形式中,我仔细检查了它),但无论如何都无法翻译。 LOCALE_PATH有效:
>>> settings.LOCALE_PATH
'/home/mefioo/apps/generator/src/locale/'
我已经 运行 'compilemessages':
locale/
└── en
└── LC_MESSAGES
├── django.mo
└── django.po
有什么想法吗?
编辑:在 MIDDLEWARE_CLASSES 中排序 - 在 session 之后,在通用之前,仍然无法正常工作。
编辑:根据@Andrea Corbellini 的建议,我也为'pl' 做了翻译,msgstr
等于msgid
,仍然没有。我如何检查 Django 是否找到语言环境文件,即使我确定 LOCALE_PATH 是正确的?
好吧,我瞎了,我的大脑欺骗了我,没有 LOCALE_PATH
这样的东西,有 LOCALE_PATHS
。我添加了这个小 'S',现在一切正常。 (我要回小学再学读书,有问题请见谅。)