I want to implement a Language switch in a website. The language switch should work as a dropdown menu. The website shows the Language switch, but it doesn’t work.
Here is the navigation:
<nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm fixed-top">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
{{ range .Site.Menus.main }}
{{ if .HasChildren }}
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle{{ if $.HasMenuCurrent " main" . }} active{{ end }}" href="#"
role="button" data-bs-toggle="dropdown" aria-expanded="false">
{{ .Pre }}<span>{{ .Name }}</span>{{ .Post }}
</a>
<ul class="dropdown-menu">
{{ range .Children }}
{{ if eq .Params.divider true }}
<li>
<hr class="dropdown-divider">
</li>
{{ else }}
<li>
<a class="dropdown-item{{ if $.IsMenuCurrent " main" . }} active"
aria-current="page{{ end }}" href="{{ .URL }}">
{{ .Pre }}<span>{{ .Name }}</span>{{ .Post }}
</a>
</li>
{{ end }}
{{ end }}
</ul>
</li>
{{ else }}
<li class="nav-item">
<a class="nav-link{{ if $.IsMenuCurrent " main" . }} active" aria-current="page{{ end }}"
href="{{ .URL }}">
{{ .Pre }}<span>{{ .Name }}</span>{{ .Post }}
</a>
</li>
{{ end }}
{{ end }}
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<img src="/images/lang.svg" alt="Language">
</button>
<ul class="dropdown-menu" aria-labelledby="navbarDropLanguages">
{{ range $.AllTranslations }}
<li><a class='dropdown-item {{ if eq .Lang $.Lang }} active {{ end }}'
href='{{ .RelPermalink }}' hreflang="{{ $.Lang }}"> {{ .Language.LanguageName }} </a>
</li>
{{ end }}
</ul>
</ul>
<a class="navbar-brand" href="/">{{ .Site.Title }}<img src="/images/logo.jpg" class="logo"
alt="{{ .Site.Title }}">
</a>
</div>
</div>
</nav>
The config-yaml has the following entries:
baseURL: https://example.com/
relativeURLs: true
languageCode: en-us
title: Example AG
theme: example
defaultContentLanguage: en
languages:
en: {weight: 1, languagename: English}
de: {weight: 2, languagename: Deutsch}
module:
imports:
- path: github.com/gohugoio/hugo-mod-bootstrap-scss/v5
markup:
goldmark:
renderer:
unsafe: true
The translation is created with Weblate. German is the preferred language!
de.json
{
"Mehr lesen": "Mehr lesen",
"404 Nicht gefunden": "404 Nicht gefunden",
"Leider konnte die Seite, die Sie aufgerufen haben, nicht gefunden werden.": "Leider konnte die Seite, die Sie aufgerufen haben, nicht gefunden werden."
}
en.json
{
"Mehr lesen": "Read more",
"404 Nicht gefunden": "404 Not Found",
"Leider konnte die Seite, die Sie aufgerufen haben, nicht gefunden werden.": "Sorry, but the page you were trying to access could not be found."
}
My 404 Page as an example
{{define "main"}}
<div class="container">
<div class="py-5">
<section class="d-flex flex-column flex-fill align-items-center justify-content-center py-3">
<h1 class="section-title"> {{ i18n "404 Nicht gefunden" }} </h1>
<div>
{{ i18n "Leider konnte die Seite, die Sie aufgerufen haben, nicht gefunden werden." }}
</div>
</section>
</div>
</div>
{{end}}