update language switcher, ref: #162

This commit is contained in:
Somrat 2024-12-18 15:29:09 +06:00
parent b37198bcc7
commit 2c17174c8c
2 changed files with 31 additions and 24 deletions

View File

@ -1,27 +1,34 @@
<!-- Language List --> <!-- Language List -->
{{ if hugo.IsMultilingual }}
{{ $class := .Class }} {{ $class := .Class }}
{{ $context := .Context }} {{ $context := .Context }}
{{ $pageLang := $context.Lang }} {{ $pageLang := $context.Lang }}
{{ $base:= urls.Parse site.Home.Permalink }} {{ $pageTranslations := newScratch }}
{{ $siteLanguages := site.Home.AllTranslations }} {{/* First, fill all translations of the Home page (failsafe) */}}
{{ $pageLink := replace (replace $context.RelPermalink (add $pageLang "/") "") $base.Path "/" }} {{ range site.Home.AllTranslations }}
{{ $pageTranslations.Set .Language.Lang .Permalink }}
{{ end }}
{{/* Second, if a translation exists for the current page for the target language, replace failsafe */}}
{{ range $context.AllTranslations }}
{{ $pageTranslations.Set .Language.Lang .Permalink }}
{{ end }}
{{ if $context.IsTranslated }}
<select class="{{ $class }}" onchange="location = this.value"> <select class="{{ $class }}" onchange="location = this.value">
{{ range $siteLanguages }} {{ range site.Languages }}
{{ if eq (string $pageLang) (string .Language) }} {{/* Fill the dropdown with all known languages */}}
{{ $link := $pageTranslations.Get .Lang }}
{{ if $link }}
<option <option
id="{{ .Language }}" id="{{ .Lang }}"
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}" value="{{ $link }}"
selected> {{ if eq .Lang $pageLang }}
{{ .Language.LanguageName }} selected
{{ end }}
>
{{ .LanguageName }}
</option> </option>
{{ else }} {{ else }}
<option {{/* if we can't safely redirect the user to the translated page or at least to translated home, discard the language from options */}}
id="{{ .Language }}"
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}">
{{ .Language.LanguageName }}
</option>
{{ end }} {{ end }}
{{ end }} {{ end }}
</select> </select>

View File

@ -1,7 +1,7 @@
{ {
"name": "hugoplate", "name": "hugoplate",
"description": "hugo tailwindcss boilerplate", "description": "hugo tailwindcss boilerplate",
"version": "1.18.0", "version": "1.18.1",
"license": "MIT", "license": "MIT",
"author": "zeon.studio", "author": "zeon.studio",
"scripts": { "scripts": {