Reputation: 1105
As suggested, I'm wanting to show a language prefix in the URL and allow a visitor to my site to change the language using a form in the template. I've been using the sections on URL prefix and set_language redirect view from the "Translation" page of the Django docs as a guide so far. My code is largely copied from this source.
Without using "i18n_pattern" (having both "url" calls in "pattern") in my URLconf, I manage to select language on the index.html page and have Django redirect me to the same page with the new language I have chosen. However, of course, the language URL prefix isn't shown.
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'm set on English and I change from "English (en)" to "Türkçe (tr)" in the form, the page essentially does a refresh without changing to Turkish (English is still shown). I can however change the language by changing my 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'm using are located in the app called "pages".
Any help on getting these to work together would be appreciated! I have so far been unable to find anything on stackoverflow relating to this issue. If any more information is needed, please ask!
Upvotes: 3
Views: 1923
Reputation: 1105
This StackOverflow question contains further details and some possible answers to the problem. None of the solutions are really appealing; nor should really be required. Unfortunately, this seems to be a problem with Django where both desired "features" can't 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 negate the problem.
Upvotes: 1