Ok, so slowly we arrive somewhere
- You are in a Spanish post
- you link to another Spanish post
- that post does not exist, so you expect a link to english
right? In that case English is the sites main language, you have the following somewhere in your config:
[languages]
[languages.en]
title = 'My blog'
weight = 1
If your English is en-us or anything else, keep that on notes.
I don’t see any way to get the defaultContentLanguage
from the root section of your config, so we go a “hard-coded” way for now.
NOTE: this is hobbled together and untested, so don’t give up immediately if it does not work. MIght have typoes.
call this layouts/shortcodes/langref.html
for now
{{- $reference := .Get "reference" -}}
{{- $defaultlanguage := "en" -}}{{- /* <-- put your fallback language code here */ -}}
{{- $language := .Get "language" | default $defaultlanguage -}}
{{- $context := $ -}}{{- /* <-- I could be wrong with this one, but inside of the shortcode it should be the current page */ -}}
{{- with site.GetPage $reference }}{{- /* loading the referenced page */ -}}
{{- if .IsTranslated }}{{- /* checking if the page has translations */ -}}
{{- with index .Translation $language }}{{- /* translation is available, so let's do this */ -}}
{{- ref $context (dict "path" $reference "lang" $language) -}}
{{- else -}}
{{- ref $context (dict "path" $reference "lang" $defaultlanguage) -}}
{{- end -}}
{{- else -}}{{- /* no translations, so let's print out what ref would do at this point */ -}}
{{- ref $context $reference -}}
{{- end -}}
{{- else -}}{{- /* referenced page is not available, so let's print out what ref would do at this point */ -}}
{{- ref $context $reference -}}
{{- end -}}
Then call it with {{< langref reference="/about.md" lang="es" >}}
.
What it should do:
- check if “/about.md” exists or go into “blind” ref creation (as if you called {{< ref “about.md”>}})
- check if about.md is translated or return the default reference
- check if language exist and either return that or the default language
Keep an eye on your template tags, those -
everywhere will result in the shortcode returning only your HTML in the end, not plenty of empty new lines.
Also, not 100% sure if that returns a formatted link or just an URL, you might need to add some HTML at the end.
Edit: I fixed some obvious errors in the shortcode. Still not sure if it works, I have no language site.