1

I've to do the switch language for an app who have some urls in i18n pattern like all the pages we want to translate with prefix in order to have good SEO, and other pages which we don't want to have the prefix in the url.

So I separated the 2 patterns, till this all is all right and when I'm doing the form to handle the switch language I'm facing only problems :

This is my form :

<form action="{% url 'set_language' %}" method="POST" class="langform" style="display:none;">
{% csrf_token %}
<input name="next" type="hidden" value="{{ request.get_full_path|strip_lang }}" />
<input name="language" id="language" type="hidden" value="" />
</form>

I validate via javascript. All is fine, I fill the language value and the form is well submit. My real problem is the next value, because I need a generic next value so I did a request.path with a filter like this one :

@register.filter(name="strip_lang")
    def strip_lang(value):
        lang = get_language()
        return '/%s/' % value.lstrip('/%s/' % lang)

The goal of this filter is to watch if the url have a language prefix if yes, it remove it to have in the next value the only the url without prefix.

the output of this filter is like this : if we are at /fr/pages/default it will be /pages/default/

I click on the switch language but nothing work. It still the same language. So I removed a slash but then it's a 404 because it's not the right path...

What can I do to manage the 2 kind of urls that I've ?? One with URL prefix and one without...

The language is well activate in some of the situations.

Thank in advance!

4

2 回答 2

0

好的,所以我设法让它工作,我只需将 {% language lang_code %} 添加到我的链接中。

现在我的网址正在使用通用的下一个值。

<ul class="lang-switch">
            {% get_language_info_list for LANGUAGES as languages %}
              {% for language in languages %}
              {% language lang_code %}
              <li><a href="#" rel="{{ language.code }}">{{ language.code|upper }}</a></li>
              {% endlanguage %}
              {% endfor %}
        </ul>

我只是发布了我的小 javascript,它可以帮助您使其工作并了解我是如何做到这一点的:

  $('.lang-switch li a').click(function (e) {
e.preventDefault;
var lang = $(this).attr('rel');
$('.langform #language').val(lang);
$('.langform').submit();
});
于 2015-01-12T10:31:15.587 回答
0
@register.filter(name="strip_lang")
def strip_lang(value):
    lang = get_language()
    return value.replace('/{0}/'.format(lang), '/')

为我工作。

于 2015-07-28T19:09:18.167 回答