Reputation: 1609
I am using Django and I want to change the site language when users click a button for example. I can get the current language code using {% get_current_language as LANGUAGE_CODE %}
but how can I change it from a template?
Upvotes: 1
Views: 1279
Reputation: 460
<form name="setLangEnglish" action="/i18n/setlang/" method="POST">
{% csrf_token %}
<input name="next" type="hidden" value="{{request.path}}"/>
<input type="hidden" name="language" value="en"/>
</form>
What this form actually does is changing your current language to "en" which is english.Since this form input type is hidden,you wouldn't see form in html. What you have to do is find out how you would like to change language(with clicking text or clicking country flag.).Here is an example for changing langugage with clicking text.
<a onclick="document.setLangEnglish.submit();return false;">ENG</a>
This code submit the form above whenever user clicks "ENG".In order to be this working, make sure that url(r'^i18n/',include('django.conf.urls.i18n')),
is in the urls.py
.
Upvotes: 2