Please wait...

keyboard_arrow_up

Change language in template using templatetag in Django

by in Python

It will help for change URL in available languages in Django templates


from django import template
from django.core.urlresolvers import resolve, reverse, Resolver404
from django.utils.translation import get_language, activate

register = template.Library()
@register.simple_tag(takes_context=True)
def change_lang(context, lang=None, *args, **kwargs):
    """
    Get active page's url by a specified language
    Usage: {% change_lang 'en' %}
    """
    path = context['request'].path
    full_path = context['request'].get_full_path()
    try:
        url_parts = resolve(path)
        cur_language = get_language()
        try:
            activate(lang)
            url = reverse(url_parts.view_name, kwargs=url_parts.kwargs)
            activate(cur_language)
            parameters = "?{0}".format(full_path.split('?')[1]) if len(full_path.split('?')) == 2 else ""
            return "{0}{1}".format(url, parameters)
        except Exception:
            pass
    except Resolver404:
        pass
    return full_path
{% load <filename> %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
     <a href="{% change_lang language.code %}">
         {{ language.name_local }}
     </a>
{% endfor %}