Django 1.10:翻译/本地化不切换语言(使用非默认语言?)

时间:2017-06-06 06:05:31

标签: python django translation

我正在开发一个网络应用程序,我希望能够使用Django's i18n functionalities在专门的英语和菲律宾语(他加禄语)之间切换。现在,我的网页实际上只是"你好",但我希望它显示菲律宾语翻译(" Kamusta"),我试图实现我的view.py,以便它发生。但是,它仍然只是说"你好",并且没有切换。这是我到目前为止所拥有的。

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'example/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',
                'django.template.context_processors.i18n',
            ],
        },
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = (
    ('en', _('English')),
    ('tl', _('Tagalog')),
)

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

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from django.utils.translation import LANGUAGE_SESSION_KEY
from django.utils import translation

# Create your views here.
def index(request):
    translation.activate("tl")
    request.session[LANGUAGE_SESSION_KEY] = "tl"
    return render(request, "index.html")

的index.html:

{% load i18n %}

<!DOCTYPE html>

<html>

<head>
  <title>Example</title>
</head>

<body>
  {% trans "Hello world!" %}
</body>

</html>

django.po:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-06-06 04:40+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: example/templates/index.html:12
msgid "Hello"
msgstr "Kamusta"

#: translate/settings.py:120
msgid "English"
msgstr ""

#: translate/settings.py:121
msgid "Tagalog"
msgstr ""

我一直在挖掘,据我所知,菲律宾人不是默认的语言&#34; Django承认。这是否意味着我根本无法翻译?我的某些人认为必须有办法。世界上有这么多语言,Django绝不支持其中40种语言的翻译。

0 个答案:

没有答案
相关问题