私はウェブサイトを国際化しました。異なる言語のプレフィックス、すなわち/ en/homeと/ de/homeがそれぞれの言語で動作する同じURLを訪問して翻訳を行っています。言語の切り替え時にDjangoの国際化が正しくリダイレクトされないのですか?
Django built-in view setlangを使用して言語を切り替えると、言語プレフィックス、つまり/ en/homeに変更を加えずにリダイレクトURLが返され、ドイツ語に切り替えると/ de/home /にリダイレクトされるはずです/ ja/home
Django内でいくつかのデバッグを行い、Django内を突っ込んでいると、translate_url()という関数が適切なURLを正しく返していないことがわかりました。残念なことに、深く行くと私のために少し毛深くなり、私は私の髪を裂いています。上の関数名をクリックすると、私に問題を与えている正確な行が得られます。
誰か手掛かりがありますが間違っているかもしれませんか?
urls.py:
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += i18n_patterns(
url(r'^', include('website.urls')),
)
ウェブサイト/ urls.py:
urlpatterns = [
url(r'^contact/$',
ContactFormView.as_view(form_class=CustomContactForm),
name='contact_form'),
url(r'^contact/sent/$',
TemplateView.as_view(
template_name='contact_form/contact_form_sent.html'),
name='contact_form_sent'),
url(r'^$', TemplateView.as_view(template_name="website/home.html")),
]
テンプレート:
{% load staticfiles %}
{% load i18n %}
{% load website_tags %}
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES_AVAILABLE %}
<form action="{% url 'set_language' %}" method="post">
<div class="language_choice">
<label for="language">Language</label>
<div class="lang_drp">
{% csrf_token %}
<select name="language" id="language">
{% for code, name in LANGUAGES_AVAILABLE %}
<option value="{{ code }}" {% if code == LANGUAGE_CODE %}selected{% endif %}>{{ name }}</option>
{% endfor %}
</select>
</div>
</div>
</form>
settings.py:
# MIDDLEWARE CONFIGURATION
# -----------------------------------------------------------------------------
MIDDLEWARE_CLASSES = [
'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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# TEMPLATE CONFIGURATION
# -----------------------------------------------------------------------------
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(APPS_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',
'django.template.context_processors.i18n',
],
},
},
]
# LANGUAGE CONFIGURATION
# -----------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/topics/i18n/
USE_I18N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'de'
# Language name string is always native, that is to ensure that if reader don't
# understand the current language displayed, the user would still be able to
# recognize the native name of their language.
LANGUAGES = [
('en', 'English'),
('de', 'Deutsch'),
]
LOCALE_PATHS = [
os.path.join(APPS_DIR, 'locale'),
]