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,28 +1,35 @@
<!-- 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 }}
{{ if $context.IsTranslated }}
<select class="{{ $class }}" onchange="location = this.value">
{{ range $siteLanguages }}
{{ if eq (string $pageLang) (string .Language) }}
<option
id="{{ .Language }}"
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}"
selected>
{{ .Language.LanguageName }}
</option>
{{ else }}
<option
id="{{ .Language }}"
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}">
{{ .Language.LanguageName }}
</option>
{{ end }}
{{ end }}
</select>
{{ end }} {{ 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 }}
<select class="{{ $class }}" onchange="location = this.value">
{{ range site.Languages }}
{{/* Fill the dropdown with all known languages */}}
{{ $link := $pageTranslations.Get .Lang }}
{{ if $link }}
<option
id="{{ .Lang }}"
value="{{ $link }}"
{{ if eq .Lang $pageLang }}
selected
{{ end }}
>
{{ .LanguageName }}
</option>
{{ else }}
{{/* if we can't safely redirect the user to the translated page or at least to translated home, discard the language from options */}}
{{ end }}
{{ end }}
</select>
{{ end }}

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": {