Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language selection templatetag #137

Closed
sunjoomoon opened this issue Mar 23, 2016 · 1 comment
Closed

Language selection templatetag #137

sunjoomoon opened this issue Mar 23, 2016 · 1 comment

Comments

@sunjoomoon
Copy link

Do we have any templatetag for changing/ switching language in the template? That is for the site visitors to select a language, if multilingual site. I've tried many but none works well.

@sunjoomoon
Copy link
Author

For multilanguage selection, I tried many of course including django doc as per https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#the-set-language-redirect-view

The thing was it failed to move to the selected language... all were fine except for that.

After googling around, I found below answer...

<form action="{% url 'set_language' %}" method="post" id="lang_changer">
{% 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>
<script>$('#lang_changer input[name="next"]').attr('value', '/'+window.location.pathname.substring(4));</script>

Thanks to http://stackoverflow.com/users/1242538/fenume and see statckoverflow http://stackoverflow.com/questions/11437454/django-templates-get-current-url-in-another-language/22318420#22318420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant