2

I have followed the approach described here.

On the index page I have a form which allows me to switch between website languages. I have added "next" post attribute enable redirection to the correct language version of the page.

<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="language" type="hidden" value="{{ language.code }}" />
<input name="next" type="hidden" value="/{{ language.code }}/{{ request.path|slice:"4:" }}" />
<input type="submit" value="{{ language.code }}"
class="btn-link{% if language.code == LANGUAGE_CODE %} current{% endif %}" /></form>

I have my own set_language view as described in the first link. Below is the last part of the view which stores LANGUAGE_SESSION_KEY in session.

if language and check_for_language(language):
    if hasattr(request, 'session'):
        request.session[LANGUAGE_SESSION_KEY] = language
    else:
        response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
return response

My urls.py looks like:

urlpatterns = i18n_patterns("",
    ("^admin/", include(admin.site.urls)),

    ("^", include("mezzanine.urls")),

    url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),
)

In general approach is working fine. When the user is on the index page he can change the language and is redirected to the correct page.

The first problem is with Mezzanine Links. If user clicks on the link then he is redirected to the language defined by LANGUAGE_CODE in settings.py. At the same time if user clicks on the menu item of the Mezzanine Page then everything is ok.

Second problem is when user clicks on the Mezzanine home link. In this case user is also redirected to the version of the site defined by LANGUAGE_CODE.

The question is why after I set LANGUAGE_SESSION_KEY in session it doesn't have any effect on future rendering of pages in correct language?

Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65

1 Answers1

1

I have solved my problem in following way. Firstly, changed the form:

<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="language" type="hidden" value="{{ language.code }}" />
{% if request.path|slice:"4:"|length > 0 %}
    <input name="next" type="hidden" value="/{{ language.code }}/{{ request.path|slice:"4:" }}" />
{% else %}
    <input name="next" type="hidden" value="/" />
{% endif %}
<input type="submit" value="{{ language.code }}"
class="btn-link{% if language.code == LANGUAGE_CODE %} current{% endif %}" />
</form>

If user picks the language from the main page then next value will be "/", if user changes the language on other pages then it changes the language prefix.

Also I don't use Links in Mezzanine anymore, because they are not resolved correctly according to the current language. Instead of Links I have created normal Mezzanine Pages.

Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65