about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-02-18 14:59:47 +0100
committerAlan Pearce2024-02-18 20:40:21 +0100
commitd604fa65ddad76100d9061d35b4129e6c6c5771d (patch)
tree61114fffbf69c46b697462ec3aa6c3858ac28572
parenta6000685cfcbb4253f634b44637b599cec96d5bc (diff)
downloadzola-bearblog-d604fa65ddad76100d9061d35b4129e6c6c5771d.tar.lz
zola-bearblog-d604fa65ddad76100d9061d35b4129e6c6c5771d.tar.zst
zola-bearblog-d604fa65ddad76100d9061d35b4129e6c6c5771d.zip
Create optional language switcher
-rw-r--r--config.toml14
-rw-r--r--templates/header.html1
-rw-r--r--templates/language_switcher.html11
-rw-r--r--templates/nav.html9
4 files changed, 29 insertions, 6 deletions
diff --git a/config.toml b/config.toml
index 5a54517..92637f3 100644
--- a/config.toml
+++ b/config.toml
@@ -44,23 +44,29 @@ highlight_code = true
 [extra]
 date_format="%d %b, %Y"
 webserver_sends_csp_headers=true
+language_switcher=true
+
+translations = [
+  { code="en", name="English" },
+  { code="de", name="Deutsch" },
+]
 
 [[extra.main_menu]]
-name_en = "Home"
+name = "Home"
 name_de = "Startseite"
 url = "/"
 
 [[extra.main_menu]]
-name_en = "Bear"
+name = "Bear"
 name_de = "Bear"
 url = "@/bear.md"
 
 [[extra.main_menu]]
-name_en = "Zola"
+name = "Zola"
 name_de = "Zola"
 url = "@/zola.md"
 
 [[extra.main_menu]]
-name_en = "Blog"
+name = "Blog"
 name_de = "Blog"
 url = "@/blog/_index.md"
diff --git a/templates/header.html b/templates/header.html
index f92e903..6e56025 100644
--- a/templates/header.html
+++ b/templates/header.html
@@ -1,4 +1,5 @@
 <header>
+  {% include "language_switcher.html" %}
   <a href="{{ get_url(path="@/_index.md", lang=lang) }}" class="title">
     <h1>{{ config.title }}</h1>
   </a>
diff --git a/templates/language_switcher.html b/templates/language_switcher.html
new file mode 100644
index 0000000..b82ddfc
--- /dev/null
+++ b/templates/language_switcher.html
@@ -0,0 +1,11 @@
+{% if config.extra.language_switcher %}
+  <nav>
+    {%- for tr in config.extra.translations %}
+      {%- if tr.code == lang %}
+        <strong>{{ tr.name }}</strong>
+      {%- else %}
+        <a href="{{ get_url(path="/", lang=tr.code) }}">{{ tr.name }}</a>
+      {%- endif %}
+    {%- endfor %}
+  </nav>
+{% endif %}
diff --git a/templates/nav.html b/templates/nav.html
index cd2d391..ac3f44e 100644
--- a/templates/nav.html
+++ b/templates/nav.html
@@ -7,10 +7,15 @@
 {%- if config.extra.main_menu %}
   {%- for item in config.extra.main_menu %}
     {% set language_key = 'name_' ~ current_lang %}
+    {% if current_lang == config.default_language %}
+      {% set title = item | get(key=language_key, default=item.name) %}
+    {% else %}
+      {% set title = item[language_key] %}
+    {% endif %}
     {%- if item.url is matching("https?://") %}
-      <a href="{{ item.url }}">{{ item[language_key] }}</a>
+      <a href="{{ item.url }}">{{ title }}</a>
     {%- else %}
-      <a href="{{ get_url(path=item.url, lang=lang )}}">{{ item[language_key] }}</a>
+      <a href="{{ get_url(path=item.url, lang=lang )}}">{{ title }}</a>
     {%- endif %}
   {%- endfor %}
 {%- endif -%}