generating language dropdown urls for nav with regex

This commit is contained in:
Christoph J. Scherr 2023-06-06 22:27:30 +02:00
parent 4c7f60fab7
commit 7a26ceae3e
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
6 changed files with 18 additions and 71 deletions

View File

@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %}
{% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %}
{% block main %}
<div class="jumbotron text-center">
<h1>400 {% translate "Bad request" %}</h1>
<p>{% translate "You sent bad data to the server." %}</p>
</div>
<div class="container text-center jumbotron my-5" py-5>
<h1 class="my-4">{% translate "Looking for anything specific?" %}</h1>
{% include 'main_search_form.html' %}
</div>
{% endblock main %}

View File

@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %}
{% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %}
{% block main %}
<div class="jumbotron text-center">
<h1>403 {% translate "Permission denied" %}</h1>
<p>{% translate "You are not allowed to access this page." %}</p>
</div>
<div class="container text-center jumbotron my-5" py-5>
<h1 class="my-4">{% translate "Looking for anything specific?" %}</h1>
{% include 'main_search_form.html' %}
</div>
{% endblock main %}

View File

@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %}
{% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %}
{% block main %}
<div class="jumbotron text-center">
<h1>404 {% translate "Not found" %}</h1>
<p>{% translate "The resource you are looking for does not exist." %}</p>
</div>
<div class="container text-center jumbotron my-5" py-5>
<h1 class="my-4">{% translate "Looking for anything specific?" %}</h1>
{% include 'main_search_form.html' %}
</div>
{% endblock main %}

View File

@ -1,15 +0,0 @@
{% extends 'base.html' %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% block languagecode %}{{ LANGUAGE_CODE }}{% endblock languagecode %}
{% block title %}{% translate "cscherr.de" %} - {% translate "startpage" %}{% endblock title %}
{% block main %}
<div class="jumbotron text-center">
<h1>500 {% translate "Internal Server Error" %}</h1>
<p>{% translate "Something went wrong." %}</p>
</div>
<div class="container text-center jumbotron my-5" py-5>
<h1 class="my-4">{% translate "Looking for anything specific?" %}</h1>
{% include 'main_search_form.html' %}
</div>
{% endblock main %}

View File

@ -51,7 +51,7 @@
<ul class="dropdown-menu">
{% for lang_code, lang_name in languages %}
<li><a class="dropdown-item"
href="{{ request.scheme }}://{{ request.META.HTTP_HOST }}{% change_lang lang_code %}?{{ request.GET.urlencode }}">
href="{% change_lang lang_code %}">
{{ lang_name }}</a></li>
{% endfor %}
<li><hr class="dropdown-divider"></li>

View File

@ -2,11 +2,16 @@ from django.template import Library
from django.urls import resolve, reverse
from django.utils.translation import activate, get_language
import re
import logging
logger = logging.getLogger(__name__)
register = Library()
@register.simple_tag(takes_context=True)
def change_lang(context, lang=None, *args, **kwargs):
def change_lang(context, lang="de", *args, **kwargs):
"""
Get active page's url by a specified language
Usage: {% change_lang 'en' %}
@ -15,16 +20,18 @@ def change_lang(context, lang=None, *args, **kwargs):
https://stackoverflow.com/a/41147772
"""
path = context['request'].path
url_parts = resolve(path)
path = context['request'].get_raw_uri()
logger.debug(f"requestdir: {dir(context['request'])}")
url = path
cur_language = get_language()
try:
activate(lang)
url = reverse(url_parts.view_name, kwargs=url_parts.kwargs)
cur_lang: str = get_language()
url = re.sub(f"/{cur_lang}/", f"/{lang}/", url)
except Exception as e:
logger.error(f"exception while building language switcher form: {e}")
logger.debug(f"this is the context: {context}")
finally:
activate(cur_language)
return "%s" % url
activate(lang)
return "%s" % url