Improve url handling within the navbar

This commit is contained in:
Rodolphe Breard 2018-01-28 12:09:15 +01:00
parent 0b59b4b140
commit 22f7f217cf
2 changed files with 20 additions and 14 deletions

View file

@ -1,5 +1,5 @@
{% load static %}<nav class="navbar navbar-expand-lg navbar-light" id="mainNav">
<a class="navbar-brand" href="/{{ current_lang_code }}/">
<a class="navbar-brand" href="{% url 'index' %}">
<img src="{% static "images/icon_khaganat.png" %}" alt="Khaganat">
</a>
<div class="collapse navbar-collapse" id="navbarNav">
@ -32,10 +32,10 @@
</div>
<ul class="navbar-nav flex-row ml-md-auto d-none d-md-flex">
<li class="nav-item dropdown">
<a class="nav-item nav-link dropdown-toggle mr-md-2" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ current_lang_name }}</a>
<a class="nav-item nav-link dropdown-toggle mr-md-2" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ current_lang_name|capfirst }}</a>
<div class="dropdown-menu dropdown-menu-right">
{% for lang_code, lang_name in all_langs %}
<a class="dropdown-item{% if lang_code == current_lang_code %} active{% endif %}" href="/{{ lang_code }}/{{ current_url }}">{{ lang_name }}</a>
{% for lang_code, lang_name, lang_url in all_langs %}
<a class="dropdown-item{% if lang_code == current_lang_code %} active{% endif %}" href="{{ lang_url }}">{{ lang_name|capfirst }}</a>
{% endfor %}
</div>
</li>

View file

@ -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)