From 78c0f712c12188992146ea9c93033a5e189cb548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar?= Date: Thu, 14 Sep 2023 00:38:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8D=EF=B8=8F=20feat:=20enhance=20SEO?= =?UTF-8?q?=20and=20accessibility=20with=20meta=20tags=20(#154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/partials/header.html | 21 ++++++++++--- templates/partials/multilingual_tags.html | 37 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 templates/partials/multilingual_tags.html diff --git a/templates/partials/header.html b/templates/partials/header.html index 26d1ffb..943e1fb 100644 --- a/templates/partials/header.html +++ b/templates/partials/header.html @@ -15,7 +15,7 @@ {% endif %} {# Feed #} - {# CSS #} @@ -79,19 +79,32 @@ {% endif %} - + + {# Add og:locale and hreflang tags for multilingual sites #} + {%- if config.languages | length > 0 %} + {%- include "partials/multilingual_tags.html" -%} + {%- else -%} + + {%- endif %} + {# Image for social media sharing #} {%- set social_media_card = macros_settings::evaluate_setting_priority(setting="social_media_card", page=page | default(value=""), section=section | default(value=""), default_global_value="") -%} {% if social_media_card %} + {%- set meta = get_image_metadata(path=social_media_card, allow_missing=true) -%} + {%- if meta -%} + + + {%- endif -%} {% endif %} - {% set current_url = current_url | default(value="/") %} - + {%- if current_url -%} + + {%- endif -%} diff --git a/templates/partials/multilingual_tags.html b/templates/partials/multilingual_tags.html new file mode 100644 index 0000000..12357ef --- /dev/null +++ b/templates/partials/multilingual_tags.html @@ -0,0 +1,37 @@ +{# 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="") -%} + + {# Loop through all available languages in config #} + {%- for lcode, ldetails in config.languages -%} + {%- set translated_filename = base_filename -%} + + {# If the language is not the default language, append the language code to the base filename #} + {%- 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) -%} + + {# 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 -%} + {%- set other_language_strings = load_data(path="themes/tabi/i18n/" ~ lcode ~ ".toml", required=false) -%} + {%- endif -%} + + + {# Construct href for hreflang #} + {%- set href = translated_page.permalink -%} + {%- if lcode == config.default_language -%} + {%- set href = href | replace(from='/' ~ lang ~ '/', to = '/') -%} + {%- else -%} + {%- set href = href | replace(from='/' ~ lang ~ '/', to='/' ~ lcode ~ '/') -%} + {%- endif -%} + + {%- endif -%} + {%- endfor -%} +{%- endif -%}