update language switcher, ref: #162
This commit is contained in:
parent
b37198bcc7
commit
2c17174c8c
@ -1,27 +1,34 @@
|
||||
<!-- Language List -->
|
||||
{{ if hugo.IsMultilingual }}
|
||||
{{ $class := .Class }}
|
||||
{{ $context := .Context }}
|
||||
{{ $pageLang := $context.Lang }}
|
||||
{{ $base:= urls.Parse site.Home.Permalink }}
|
||||
{{ $siteLanguages := site.Home.AllTranslations }}
|
||||
{{ $pageLink := replace (replace $context.RelPermalink (add $pageLang "/") "") $base.Path "/" }}
|
||||
{{ $pageTranslations := newScratch }}
|
||||
{{/* First, fill all translations of the Home page (failsafe) */}}
|
||||
{{ 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">
|
||||
{{ range $siteLanguages }}
|
||||
{{ if eq (string $pageLang) (string .Language) }}
|
||||
{{ range site.Languages }}
|
||||
{{/* Fill the dropdown with all known languages */}}
|
||||
{{ $link := $pageTranslations.Get .Lang }}
|
||||
{{ if $link }}
|
||||
<option
|
||||
id="{{ .Language }}"
|
||||
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}"
|
||||
selected>
|
||||
{{ .Language.LanguageName }}
|
||||
id="{{ .Lang }}"
|
||||
value="{{ $link }}"
|
||||
{{ if eq .Lang $pageLang }}
|
||||
selected
|
||||
{{ end }}
|
||||
>
|
||||
{{ .LanguageName }}
|
||||
</option>
|
||||
{{ else }}
|
||||
<option
|
||||
id="{{ .Language }}"
|
||||
value="{{ replace (add .RelPermalink $pageLink) `//` `/` }}">
|
||||
{{ .Language.LanguageName }}
|
||||
</option>
|
||||
{{/* 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>
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "hugoplate",
|
||||
"description": "hugo tailwindcss boilerplate",
|
||||
"version": "1.18.0",
|
||||
"version": "1.18.1",
|
||||
"license": "MIT",
|
||||
"author": "zeon.studio",
|
||||
"scripts": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user