我想更改网页的当前语言。
我正在使用此HTML代码制作按钮:
<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ request.get_full_path|slice:'3:' }}" />
<ul class="nav navbar-nav navbar-right language menu">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<li>
<h1>{{ language }}</h1>
<button type="submit"
name="language"
value="{{ language.code }}"
class="{% if language.code == LANGUAGE_CODE %}selected{% endif %}">
{{ language.name_local }}
</button>
</li>
{% endfor %}
</ul>
</form>
当我从西班牙语'es'切换到美国英语'en-us'时,我没有问题,但是当我尝试从美国英语切换到西班牙语时,请将此错误发送给我:
未找到: / i18n / setlang / -us /
答案 0 :(得分:0)
在你的djangp应用程序中制作teamplatetags,如下所示:
模板标签文件
firebase init
<强> HTML 强>
from django import template
from django.core.urlresolvers import resolve, reverse, Resolver404
from django.utils.translation import get_language, activate
register = template.Library()
@register.simple_tag(takes_context=True)
def change_lang(context, lang=None, *args, **kwargs):
"""
Get active page's url by a specified language
Usage: {% change_lang 'en' %}
"""
path = context['request'].path
full_path = context['request'].get_full_path()
try:
url_parts = resolve(path)
cur_language = get_language()
try:
activate(lang)
url = reverse(url_parts.view_name, kwargs=url_parts.kwargs)
activate(cur_language)
parameters = "?{0}".format(full_path.split('?')[1]) if len(full_path.split('?')) == 2 else ""
return "{0}{1}".format(url, parameters)
except Exception:
pass
except Resolver404:
pass
return full_path
http://www.neerajbyte.com/snippet/change-language-in-template-using-templatetag-in-d/