我将网站国际化并且翻译工作是通过访问具有不同语言前缀的相同网址,即/ en / home和/ de / home使用其各自的语言。
但是,当使用Django built-in view setlang切换语言时,我总是返回重定向网址而不更改语言前缀,即在/ en / home,切换到德语应该重定向到/ de / home /,但是相反,我得到/ en / home
进行一些调试,并在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'),
]
答案 0 :(得分:0)
使用 django-modeltranslation 重定向到主页更改语言后?
如果要重定向到同一个页面,可以替换这部分代码:
index.html:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<div class="languages">
<p>{% trans "Language" %}: </p>
<ul class="languages">
{% for language in languages %}
<li>
<a href="/{{ language.code }}/
{% if language.code == LANGUAGE_CODE %} class="selected"{% endif %}>
{{ language.name_local }}
</a>
</li>
{% endfor %}
</ul>
</div>
到:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<div class="languages">
<p>{% trans "Language" %}: </p>
<ul class="languages">
{% for language in languages %}
<li>
<a href="/{{ language.code }}/{{request.get_full_path|slice:"4:"}}"
{% if language.code == LANGUAGE_CODE %} class="selected"{% endif %}>
{{ language.name_local }}
</a>
</li>
{% endfor %}
</ul>
</div>
请注意:
<块引用>答案 1 :(得分:-1)
所以我发现了问题。显然,您必须包含与当前路径(没有语言前缀)对应的下一个参数/输入字段,以便重定向起作用。
我错误地认为,Django会自动解决这个问题。