diff --git a/navbar/templates/navbar/navbar.html b/navbar/templates/navbar/navbar.html index 64e5b24..b96b0f7 100644 --- a/navbar/templates/navbar/navbar.html +++ b/navbar/templates/navbar/navbar.html @@ -1,5 +1,5 @@ {% load static %} - + @@ -32,10 +32,10 @@ - {{ current_lang_name }} + {{ current_lang_name|capfirst }} - {% for lang_code, lang_name in all_langs %} - {{ lang_name }} + {% for lang_code, lang_name, lang_url in all_langs %} + {{ lang_name|capfirst }} {% endfor %} diff --git a/navbar/templatetags/navbar.py b/navbar/templatetags/navbar.py index 57376ca..ad5c711 100644 --- a/navbar/templatetags/navbar.py +++ b/navbar/templatetags/navbar.py @@ -1,8 +1,10 @@ -from django.utils.translation import activate as activate_lang +from django.utils.translation import get_language_info from django.utils.translation import gettext_lazy as _ +from django.utils.translation import activate as activate_lang from django.utils.translation import get_language from django.template.response import TemplateResponse from django.utils.safestring import mark_safe +from django.urls import reverse, resolve from django.shortcuts import render from navbar.models import Element from django.conf import settings @@ -15,13 +17,17 @@ path_re = re.compile('^(/[a-z]+)/(.*)') def get_lang_name(lang_code): + li = get_language_info(lang_code) + return li['name_local'] + + +def get_lang_data(request, lang_code, curr_lang): + lang_name = get_lang_name(lang_code) + rm = resolve(request.path_info) activate_lang(lang_code) - return str([e for e in settings.LANGUAGES if e[0] == lang_code].pop()[1]) - - -def reduce_path(path): - m = path_re.match(path) - return m.group(2) if m else path + lang_url = reverse(rm.func, args=rm.args, kwargs=rm.kwargs) + activate_lang(curr_lang) + return (lang_code, lang_name, lang_url) @register.simple_tag(takes_context=True) @@ -30,11 +36,11 @@ def navbar(context): curr_lang = get_language() ctx = { 'elems': Element.objects.filter(parent=None).order_by('weight'), - 'current_url': reduce_path(request.path_info), 'current_lang_code': curr_lang, 'current_lang_name': get_lang_name(curr_lang), - 'all_langs': [(l[0], get_lang_name(l[0])) for l in settings.LANGUAGES], + 'all_langs': [ + get_lang_data(request, l[0], curr_lang) for l in settings.LANGUAGES + ], } - activate_lang(curr_lang) tpl = TemplateResponse(request, 'navbar/navbar.html', context=ctx).render() return mark_safe(tpl.rendered_content)