无法在Django项目中更改语言

时间:2016-10-01 15:52:22

标签: python django internationalization

我正在努力改变我的Django项目中的语言。我认为一切都设置正确但它仍然无法正常工作。

我已经创建了消息并已经编译过了。

SETTINGS.PY

# -*- coding: utf-8 -*-
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__)))
SECRET_KEY = "dsadsadfasfdgfsgfds"
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'admin_interface',
    'flat',
    'colorfield',
    'django.contrib.admin',
    'django.contrib.auth',
    # 'djcelery_email',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dolava_app',
    'solo',
    'django_extensions',
    'django_tables2',
    'django_countries',
    'admin_stuff',
    'ajax_stuff',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    '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.locale.LocaleMiddleware',
]

ROOT_URLCONF = 'dolava.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.core.context_processors.i18n',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.media',

            ],
        },
    },
]

WSGI_APPLICATION = 'dolava.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/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/1.9/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.9/topics/i18n/
gettext = lambda s: s
LANGUAGES = [
    ('en','English'),
    ('sk','Slovak'),
]
LANGUAGE_CODE = 'sk'


TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

SITE_ID = 1


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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

现在我想看看transladet例如:

<div class="col-md-12">
                    <h2>{% blocktrans %}Best Way to Blah Blah Blah
                        <small>All you need to do is very simple just join us</small>{% endblocktrans %}
                    </h2>
                </div>

URLS.PY:

urlpatterns = [
    url(r'^i18n/', include('django.conf.urls.i18n')),
    ...

from django.conf.urls.i18n import i18n_patterns

urlpatterns += i18n_patterns('',)

但它仍然是英文版。你知道这可能是什么问题吗?

修改

当我尝试在视图中激活语言时,它可以正常工作(对于此页面)。

def index(request):
    from dolava import settings
    print settings.LOCALE_PATHS
    print translation.get_language()
    translation.activate('sk')
    return render(request, 'dolava/index.html')

它翻译了index页面。 的 EDIT2:

我刚才意识到,如果我从settings.py更改此代码:

LANGUAGES = [
    ('en','English'),
    ('sk','Slovak'),
]

对此:

LANGUAGES = [
    #('en','English'),
    ('sk','Slovak'),
]

所以英语被评论,斯洛伐克语翻译工作。

0 个答案:

没有答案