Render hreflang links on paginator pages in a multilingual project

Followup from topic 50340

Do you have any idea, how to deal with hreflang links on paginator pages in a mulitlanguage setup?


Right now, I have this ugly workaround, which only works if there are the same number of pages across all languages:

{{ if .IsTranslated }}
  
  {{/* TODO: Needs better solution! This workaround only works if there are the same number of pages across all languages */}}
  {{ $page_indicator := "" }}
  {{ with .Store.Get "paginator" }}
    {{ if ne .PageNumber 1 }}
      {{ $page_indicator = replace .URL page.RelPermalink "" }}
    {{ end }}
  {{ end }}

  {{ with index .AllTranslations 0 }}
    <link rel="alternate" hreflang="x-default" href="{{ .Permalink }}{{ $page_indicator }}" />
  {{ end }}
  {{ range .AllTranslations }}
    <link rel="alternate" hreflang="{{ .Lang }}" href="{{ .Permalink }}{{ $page_indicator }}" />
  {{ end }}
{{ end }}

I’ve updated the original example:

git clone --single-branch -b hugo-forum-topic-50340 https://github.com/jmooring/hugo-testing hugo-forum-topic-50340
cd hugo-forum-topic-50340
hugo server

See layouts/_partials/head/additional-metadata.html for details.

Note that index .AllTranslations 0 isn’t what you want. That’s the first translation sorted by language weight, but the first one may not be the default language.

Also note that in a future release you will be able to do this:

if .Language.IsDefault

Instead of this:

if eq .Site .Sites.Default

See #13855.