I have a two language site in Django, and a Select form to change the language of the site. It was working fine, until I started using i18n_patterns in urls. So now, every url of my site starts with: foo.com/en/ or foo.com/es/.
The problem is that when I change the language using the form, the url setting overrides the selection of the form, so the language is not beeing changed. The only way to achieve that, is doing it manually from the url.
But, I don't want to link all the language changes to the initial page. The url could be something like foo.com/en/post/foo-post/ and I would like to be able to change it to foo.com/es/post/foo-post/. What am I doing wrong?
My form is:
<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language" onchange="this.form.submit()">
<option value="es" {% if LANGUAGE_CODE == "es" %} selected="selected"{% endif %}>Español</option>
<option value="eu" {% if LANGUAGE_CODE == "en" %} selected="selected"{% endif %}>English</option>
</select>
</form>
Many thanks
I have a webpage with multilanguage selection, here is an example from my base.html template. I hope you find it useful.
{% load i18n %}
{% get_current_language as CURRENT_LANGUAGE %}
{% get_static_prefix as STATIC_PREFIX %}
<head>
...
<script>
jQuery(window).load(function() {
$('#language').change(function() {
window.location.replace("/" + $('#language').val() + "/index");
});
});
</script>
...
</head>
<body>
....
<select id="language" name="language">
{% if CURRENT_LANGUAGE == 'es' %}
<option value="es" selected="yes">{% trans "Espaniol" %}</option>
{% else %}
<option value="es">{% trans "Espaniol" %}</option>
{% endif %}
{% if CURRENT_LANGUAGE == 'en' %}
<option value="en" selected="yes">{% trans "Ingles" %}</option>
{% else %}
<option value="en">{% trans "Ingles" %}</option>
{% endif %}
</select>
...
</body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With