I added a language switch to a menu but it disappears (the select
element only, the rest of the menu stays) when I navigate away from the home page. Probably missing something basic, still learning Hugo.
This is the code in components/language-switcher.html
to switch from one language to another:
{{ $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 "/" }}
{{ 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>
<div class="select-arrow">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/>
</svg>
</div>
{{ end }}
Changing from en
to fr
works ie redirect to /fr
.
This is the menu in header.html
called from layouts/partials/essentials
:
<div class="top-menu">
<nav class="container flex relative flex-wrap items-center flex-row justify-end">
<ul class="navbar-nav order-1 hidden text-sm lg:flex w-full pb-6 lg:order-1 lg:w-auto lg:space-x-2 lg:pb-0 xl:space-x-8">
{{ $currentPage := . }}
{{ range site.Menus.topmenu }}
{{ $menuURL := .URL | absLangURL }}
{{ $pageURL:= $currentPage.Permalink | absLangURL }}
{{ $active := eq $menuURL $pageURL }}
{{ if .HasChildren }}
<li class="nav-item nav-dropdown group relative">
<span class="nav-link text-slate-100 {{ range .Children }}
{{ $childURL := .URL | absLangURL }}
{{ $active := eq $childURL $pageURL }}
{{ if $active }}active{{ end }}
{{ end }} inline-flex items-center">
{{ .Name }}
<svg class="h-4 w-4 fill-current" viewBox="0 0 20 20">
<path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z" />
</svg>
</span>
<ul class="nav-dropdown-list lg:group-hover:visible lg:group-hover:opacity-100">
{{ range .Children }}
{{ $childURL := .URL | absLangURL }}
{{ $active := eq $childURL $pageURL }}
<li class="nav-dropdown-item">
<a class="nav-dropdown-link {{ if $active }}
active
{{- end -}}" {{ if findRE `^http` .URL }} target="_blank" rel="noopener" {{ end }} href="{{- if findRE `^#` .URL -}}
{{- if not $.IsHome -}}
{{- site.Home.RelPermalink -}}
{{- end }}
{{- .URL -}}
{{- else -}}
{{- .URL | relLangURL -}}
{{- end -}}">
{{ .Name }}
</a>
</li>
{{ end }}
</ul>
</li>
{{ else }}
<li class="nav-item">
<a class="nav-link text-slate-100 hover:text-slate-300 {{ if $active }}active{{- end -}}" {{ if findRE `^http` .URL }} target="_blank"
rel="noopener" {{ end }} href="{{- if findRE `^#` .URL -}}
{{- if not $.IsHome -}}
{{- site.Home.RelPermalink -}}
{{- end }}{{- .URL -}}
{{- else -}}
{{- .URL | relLangURL -}}
{{- end -}}">{{ .Name }}</a>
</li>
{{ end }}
{{ end }}
</ul>
{{ partial "components/language-switcher" (dict "Context" . "Class" "txt-select") }}
</nav>
</div>