Skip to content

Commit

Permalink
🐛 fix: allow translated drafts (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: welpo <[email protected]>
  • Loading branch information
Smtbook and welpo authored Mar 1, 2024
1 parent 9a5ea8e commit 9041086
Showing 1 changed file with 20 additions and 42 deletions.
62 changes: 20 additions & 42 deletions templates/partials/multilingual_tags.html
Original file line number Diff line number Diff line change
@@ -1,51 +1,29 @@
{%- if section.relative_path -%}
{%- set base_path = section.relative_path-%}
{%- elif page.relative_path -%}
{%- set base_path = page.relative_path -%}
{%- if section.translations -%}
{%- set current_translations = section.translations -%}
{%- elif page.translations -%}
{%- set current_translations = page.translations -%}
{%- 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 -%}

{# 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" -%}
{%- if current_translations -%}

{# Check if the translated page or section exists #}
{%- set translation_exists = load_data(path=translated_filename, required=false) -%}
{%- for translation in current_translations -%}
{%- set lcode = translation.lang | default(value = config.default_language) -%}

{# Get the page #}
{%- if translation_exists -%}
{%- if page.relative_path -%}
{%- set translated_page = get_page(path=translated_filename, metadata_only=true) -%}
{%- else -%}
{%- set translated_page = get_section(path=translated_filename, metadata_only=true) -%}
{%- endif -%}
{#- 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 -%}
<meta property="og:locale:alternate" content="{{ macros_translate::translate(key="date_locale", default="en_GB", language_strings=other_language_strings) }}" />

{# 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 -%}
<meta property="og:locale:alternate" content="{{ macros_translate::translate(key="date_locale", default="en_GB", language_strings=other_language_strings) }}" />

{# Construct href for hreflang #}
{%- set href = translated_page.permalink | default(value=translated_page.url) -%}
{%- if lcode == config.default_language -%}
{%- set href = href | replace(from='/' ~ lang ~ '/', to = '/') -%}
{%- else -%}
{%- set href = href | replace(from='/' ~ lang ~ '/', to='/' ~ lcode ~ '/') -%}
{%- endif -%}
<link rel="alternate" hreflang="{{ lcode }}" href="{{ href | safe }}" />
{# Construct href for hreflang #}
{%- set href = translation.permalink -%}
{%- if lcode == config.default_language -%}
{%- set href = href | replace(from='/' ~ lang ~ '/', to = '/') -%}
{%- else -%}
{%- set href = href | replace(from='/' ~ lang ~ '/', to='/' ~ lcode ~ '/') -%}
{%- endif -%}
<link rel="alternate" hreflang="{{ lcode }}" href="{{ href | safe }}" />
{%- endfor -%}

{%- endif -%}

0 comments on commit 9041086

Please sign in to comment.