Failed to set language after using "i18n_pattern" to display language prefix in url

As I said, I want to show the language prefix in the url and allow the visitor to my site to change the language using a form in the template. I've used the URL prefix and set_language redirect view sections from the "Translation" page in the Django docs as a guide so far. My code is pretty much copied from this source.


Problem

Without using "i18n_pattern" (having calls to "pattern" as "url") in my URLconf, I manage to select the language on the index.html page, and Django redirects me to the same page with the new language I have selected. However, of course, the language url prefix is โ€‹โ€‹not displayed.

When I use "i18n_pattern" the language prefix appears in the url but seems to break the ability to change languages โ€‹โ€‹from the index.html form. For example, if I am set to English and I change from "English (en)" to "Tรผrkรงe (tr)" on the form, the page updates significantly without switching to Turkish (English is still displayed). However, I can change the language by changing the URL language prefix from "/ en /" to "/ tr /".

pages / templates / pages / index.html

{% load i18n %}
<html>
  <head>
  ...
  </head>
  <body>
    <form action="{% url 'set_language' %}" method="post">
    {% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}" />
    <select name="language">
    {% get_language_info_list for LANGUAGES as languages %}
    {% for language in languages %}
    <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
        {{ language.name_local }} ({{ language.code }})
     </option>
    {% endfor %}
    </select>
    <input type="submit" value="Go" />
    </form>

    ...

    </body>    
</html>

      


[project_name] /urls.py:

from django.conf.urls import patterns, include, url 
from django.conf.urls.i18n import i18n_patterns

urlpatterns = patterns('',
    url(r'^i18n/', include('django.conf.urls.i18n')),
)

urlpatterns += i18n_patterns('',
    url(r'', include('pages.urls', namespace='pages')),
)

      

Note . The templates I am using are in the appendix called "pages".

Any help with their collaboration will be appreciated! I still haven't been able to find anything on stackoverflow related to this issue. If you need more information, please ask!

+3


source to share


1 answer


This question fooobar.com/questions/670343 / ... has more information and some possible answers to the problem. None of the solutions are really attractive; and shouldn't really be required. Unfortunately, this is similar to a problem with Django, where both desired "functions" cannot work together out of the box with the desired effect. In my case, since my site is a single page site, I can get away with setting the context variable with "redirect_to" = '/' (in the view) to deny the problem.



+1


source







All Articles