getzola / zola

A fast static site generator in a single binary with everything built-in. https://www.getzola.org
https://www.getzola.org
MIT License
13.38k stars 936 forks source link

get_url returns url for wrong language #2371

Closed Toasterson closed 8 months ago

Toasterson commented 9 months ago

Bug Report

Environment

Zola version: 0.17.2

Expected Behavior

When using

<a href="{{ get_url(path=current_path, lang="de") }}">Deutsch</a>

The German Language link always points to the correct german link.

Current Behavior

In my current setup at https://gitlab.com/Toasterson/atelierwegi I have set the default Language = "de" and I am trying to get the Url of the current path for a simple language switcher. When I am in the default language every link is as expected. Once I Switch to english however the german language link is also a link to the english language. So when lang="en" in the template the language does not get correctly changed in get_url.

Step to reproduce

See https://gitlab.com/Toasterson/atelierwegi but basically.

  1. Set default_language="de"
  2. Be on a English page
  3. Use <a href="{{ get_url(path=current_path, lang="de") }}">Deutsch</a> to create a language link to the German version of the page.
  4. The link to the German version also now links to the english version

See https://atelierwegi-toasterson-620f5f93159cd6c95174bf09bc660c027f857d5e.gitlab.io/en/ Where the Link "Deutsch" in the upper left hand corner should be pointing to "/" but is pointing to "/en"

Keats commented 9 months ago

The path arg in get_url refers to the markdown file, not the URL path. I'm not sure why it doesn't error but to get it to work like you need you can do:

diff --git a/templates/base.html b/templates/base.html
index 0ebdd1e..eef63e1 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -17,8 +17,14 @@
                        </div>
                        <div class="header">
                                <div class="language">
-                                       <a href="{{ get_url(path=current_path, lang="de") }}">Deutsch</a>
-                                       <a href="{{ get_url(path=current_path, lang="en") }}">English</a>
+                                       {% if section %}
+                                               <a href="{{ get_url(path="/", lang="de") }}">Deutsch</a>
+                                               <a href="{{ get_url(path="/", lang="en") }}">English</a>
+                                       {% else %}
+                                       {% for translation in page.translations %}
+                                               <a href="{{translation.permalink}}">{% if translation.lang == "de" %}Deutsch{% else %}English{% endif %}</a>
+                                       {% endfor %}
+                                       {% endif %}
                                </div>
                                <div class="nav">
                                        <a href="{{ get_url(path="/", lang=lang) }}">{{trans(key="home", lang=lang) }}</a>
Toasterson commented 8 months ago

Oh, yeah that makes sense. Now it works thanks.