theme: announce translations as alternate links #422

This commit is contained in:
Sören Weber 2023-01-25 12:41:02 +01:00
parent 8e7bbf473e
commit 8882e2eff3
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
4 changed files with 45 additions and 24 deletions

View file

@ -27,6 +27,8 @@ theme = "hugo-theme-relearn"
# the main language of this site; also an automatic pirrrate translation is # the main language of this site; also an automatic pirrrate translation is
# available in this showcase # available in this showcase
languageCode = "en" languageCode = "en"
# make sure your defaultContentLanguage is the first one in the [Languages]
# array below, as the theme needs to make assumptions on it
defaultContentLanguage = "en" defaultContentLanguage = "en"
# if you want to get rrrid o' ourrr pirrrates nonsense uncomment th' next line # if you want to get rrrid o' ourrr pirrrates nonsense uncomment th' next line
# disableLanguages = ['pir'] # disableLanguages = ['pir']

View file

@ -20,6 +20,11 @@ After learning [how Hugo handle multilingual websites](https://gohugo.io/content
For example with current English and Piratized English website. For example with current English and Piratized English website.
{{% notice note %}}
Make sure your default language is defined as the first one in the `[Languages]` array, as the theme needs to make assumptions on it
{{% /notice %}}
```toml ```toml
# English is the default language # English is the default language
defaultContentLanguage = "en" defaultContentLanguage = "en"

View file

@ -4,20 +4,24 @@
{{- range .Data.Pages }} {{- range .Data.Pages }}
{{- if and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) }} {{- if and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) }}
<url> <url>
<loc>{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}</loc>{{ if not .Lastmod.IsZero }} <loc>{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}</loc>
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }} {{- if not .Lastmod.IsZero }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }} <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }} {{- end }}
<xhtml:link {{- with .Sitemap.ChangeFreq }}
rel="alternate" <changefreq>{{ . }}</changefreq>
hreflang="{{ .Lang }}" {{- end }}
href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" {{- if ge .Sitemap.Priority 0.0 }}
/>{{ end }} <priority>{{ .Sitemap.Priority }}</priority>
<xhtml:link {{- end }}
rel="alternate" {{- if .IsTranslated }}
hreflang="{{ .Lang }}" {{- range $index, $trans := .AllTranslations }}
href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" {{- if eq $index 0 }}
/>{{ end }} <xhtml:link rel="alternate" hreflang="x-default" href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" />
{{- end }}
<xhtml:link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" />
{{- end }}
{{- end }}
</url> </url>
{{- end -}} {{- end -}}
{{- end }} {{- end }}

View file

@ -17,25 +17,35 @@
{{- $title = printf "%s%s" $title .Site.Title }} {{- $title = printf "%s%s" $title .Site.Title }}
<title>{{ $title }}</title> <title>{{ $title }}</title>
{{- if .IsTranslated -}}
{{- range $index, $trans := .AllTranslations }}
{{- if eq $index 0 }}
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}" rel="alternate" hreflang="x-default">
{{- end }}
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}" rel="alternate" hreflang="{{ .Language.Lang }}">
{{- end }}
{{- end }}
{{- if not (and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) ) }} {{- if not (and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) ) }}
{{- else }} {{- else }}
{{- range .AlternativeOutputFormats }} {{- range .AlternativeOutputFormats }}
{{- if eq .Rel "canonical" }} {{- if eq .Rel "canonical" }}
{{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }} {{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- else if eq $outputFormat "searchpage" }} {{- else if eq $outputFormat "searchpage" }}
{{- else if eq .Name "SEARCHPAGE" }} {{- else if eq .Name "SEARCHPAGE" }}
{{- else if and (ne .Name "JSON") (ne .Name "SEARCH") }} {{- else if and (ne .Name "JSON") (ne .Name "SEARCH") }}
{{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }} {{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- end }}
{{- end }} {{- end }}
{{- end }}
{{- end }} {{- end }}
{{- if and (ne .Site.Params.disableSeoHiddenPages true) (ne .Site.Params.disableSearchHiddenPages true) }} {{- if and (ne .Site.Params.disableSeoHiddenPages true) (ne .Site.Params.disableSearchHiddenPages true) }}
{{- range .AlternativeOutputFormats }} {{- range .AlternativeOutputFormats }}
{{- if eq .Name "SEARCHPAGE" }} {{- if eq .Name "SEARCHPAGE" }}
{{- else if eq .Name "JSON" }} {{- else if eq .Name "JSON" }}
{{ (printf $link (.Permalink | relURL) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }} {{ (printf $link (.Permalink | relURL) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- end }}
{{- end }} {{- end }}
{{- end }}
{{- end }} {{- end }}
{{- partialCached "favicon.html" . }} {{- partialCached "favicon.html" . }}