From 465bc55b9ab8263f55e0f83b9468a669c81b6ce8 Mon Sep 17 00:00:00 2001 From: welpo Date: Thu, 14 Sep 2023 01:04:05 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(header):=20support=20multiling?= =?UTF-8?q?ual=20meta=20tags=20in=20sections?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds upon #154. --- templates/partials/header.html | 2 +- templates/partials/multilingual_tags.html | 24 +++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/templates/partials/header.html b/templates/partials/header.html index 943e1fb6d..406f43094 100644 --- a/templates/partials/header.html +++ b/templates/partials/header.html @@ -83,7 +83,7 @@ {# Add og:locale and hreflang tags for multilingual sites #} - {%- if config.languages | length > 0 %} + {%- if config.languages | length > 0 and current_url %} {%- include "partials/multilingual_tags.html" -%} {%- else -%} diff --git a/templates/partials/multilingual_tags.html b/templates/partials/multilingual_tags.html index 12357ef27..1e00ad6ab 100644 --- a/templates/partials/multilingual_tags.html +++ b/templates/partials/multilingual_tags.html @@ -1,7 +1,11 @@ -{# Get the base filename by stripping out any language code and '.md' suffix #} -{%- if page.relative_path -%} - {%- set base_filename = page.relative_path | replace(from=".md", to="") | replace(from="." ~ lang, to="") -%} - +{%- if section.relative_path -%} + {%- set base_path = section.relative_path-%} +{%- elif page.relative_path -%} + {%- set base_path = page.relative_path -%} +{%- endif -%} +{%- if base_path -%} + {# Get the base filename by stripping out any language code and '.md' suffix #} + {%- set base_filename = base_path | replace(from=".md", to="") | replace(from="." ~ lang, to="") -%} {# Loop through all available languages in config #} {%- for lcode, ldetails in config.languages -%} {%- set translated_filename = base_filename -%} @@ -10,13 +14,17 @@ {%- if lcode != config.default_language -%} {%- set translated_filename = base_filename ~ "." ~ lcode -%} {%- endif -%} - {%- set translated_filename = translated_filename ~ ".md" -%} - {%- set translated_page = get_page(path=translated_filename) -%} + + {%- if page.relative_path -%} + {%- set translated_page = get_page(path=translated_filename) -%} + {%- else -%} + {%- set translated_page = get_section(path=translated_filename) -%} + {%- endif -%} {# Create the og:locale and hreflang tags if the translated page exists #} {%- if translated_page -%} - + {#- Dynamically load the language strings for each language -#} {%- set other_language_strings = load_data(path="i18n/" ~ lcode ~ ".toml", required=false) -%} {%- if not other_language_strings -%} @@ -25,7 +33,7 @@ {# Construct href for hreflang #} - {%- set href = translated_page.permalink -%} + {%- set href = translated_page.permalink | default(value=translated_page.url) -%} {%- if lcode == config.default_language -%} {%- set href = href | replace(from='/' ~ lang ~ '/', to = '/') -%} {%- else -%}