Django Translation的另一个问题

时间:2015-10-09 12:10:24

标签: python django locale translation django-i18n

在阅读了几十个关于不使用transtalions的问题和答案之后,我放弃了,因为我做了(我想我做了)其他线程中提到的所有内容,但仍然无法完成工作。

我的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')),

标题中的表单(稍后将替换为不错的'点击标记'选择,但现在......)

<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(我可以看到,因为&#39;选择&#39;如果在形式中并且我仔细检查过),但无论如何都无法翻译。 LOCALE_PATH有效:

>>> settings.LOCALE_PATH
'/home/mefioo/apps/generator/src/locale/'

我已经运行了&#39; compilemessages&#39;:

locale/
└── en
    └── LC_MESSAGES
        ├── django.mo
        └── django.po

有什么想法吗?

编辑:在MIDDLEWARE_CLASSES中订购 - 在会话之后,在共同之前,仍然无效。

编辑:在@Andrea Corbellini的建议之后,我已经为&#39; pl&#39;做了翻译。同样,msgstr等于msgid,仍然没有。如何确定Django是否找到了语言环境文件,即使我确定LOCALE_PATH是否正确?

1 个答案:

答案 0 :(得分:0)

好吧,我是盲目的,我的大脑欺骗了我,没有像LOCALE_PATH这样的事情,LOCALE_PATHS。我添加了这个小小的&#39; S&#39;现在一切正常。 (我需要回到小学学习如何再次阅读,抱歉有问题。)