The language switch is not working properly

The language switch is not working properly.

I have 2 domains; example.com and example.de. Both domains have a separate directory on the webspace. When example.com is called, the English-language page should be shown and when example.de is called, the German-language page should be shown.

When calling example.com/home, example.com is correctly displayed. When changing the language, the system does not switch to example.de/start.

When calling example.com/company/ the page is displayed correctly. When switching to the German-language page, example.com/company/ is displayed and not example.de/unternehmen. The language switch is in the code of navbar.html.

I need a little help to find the mistake.

Hier die config.toml


title = ""
languageCode = "en-US"
theme = "example"

enableRobotsTXT = true
relativeUrls = true

[module]
	[[module.imports]]
		path = "github.com/gohugoio/hugo-mod-bootstrap-scss/v5"

defaultContentLanguageInSubdir = true

[languages]
  [languages.en]
    baseURL = "https://example.com"
	  languageCode = "en-US"
    languageName = "English"
    contentDir = "content/en"
  [languages.de]
    baseURL = "https://example.de"
		languageCode = "de-DE"
    languageName = "Deutsch"
    contentDir = "content/de"

[markup]
	[markup.goldmark]
		[markup.goldmark.renderer]
    		unsafe = true

head.html

<!-- Required meta tags -->
<meta charset="utf-8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
	name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<title itemprop="name">{{ .Title }} - {{ .Site.Title }}</title>
<meta property="og:title" content="{{ .Title }} - {{ .Site.Title }}" />
{{ if and (eq .Kind "taxonomy") (eq .Type "categories")}}
<meta name="robots" content="noindex, nofollow, noarchive" />
{{ else }}
<meta name="robots" content="noindex, nofollow, noarchive" />
{{ end }}

<!-- Dart Sass -->
{{ $opts := dict "transpiler" "dartsass" }}
{{ $scss := resources.Get "scss/styles.scss" | fingerprint }}
{{ $styles := $scss | resources.ToCSS $opts }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" />

<title>
	{{- if .IsHome -}}
	{{- .Site.Title -}}{{ with .Site.Params.slogan }} - {{ . }}{{ end }}
	{{- else -}}
	{{- .Title }} - {{ .Site.Title -}}
	{{- end -}}
</title>

navbar.html


<nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm fixed-top">
	<div class="container">
		<a class="navbar-brand d-lg-none ms-3" href="/">{{ .Site.Params.company }}</a>
		<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-bs-toggle="dropdown" aria-expanded="false">
						<img src="/images/lang.svg" height="20" alt="Language">
					</button>
					<ul class="dropdown-menu dropdown-menu-dark" 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 d-none d-lg-block" href="/">{{ .Site.Title }}<img src="/images/logo.svg" class="logo"
					alt="{{ .Site.Title }}"></a>
		</div>
	</div>
</nav>

Hi Joerg,

not sure about your domain name but most of the examle.*/… seem to be not yours

example.com is not your domain.
example.de redirects from 3erlei.de (about paintings) and there’s no language switch
your theme is example

viewing locally may work, cause hugo server will replace the baseURL with your localhost :wink:

seems you are still on the way to replace example code delivered with a theme to real values
checkout Multilingual mode to proceed

cheers

1 Like

Yes of course example.com and example.de are not my domains.

Your domains are accessible from everywhere and get crawled by search engines…

but anyway: for me http://example.com/home and https://example.com/home
return 404 not found

The language switch correctly translates company to unternehmen, but the baseUrl is wrong

you are on a multisite multilingual config maybe dig in here
Multilingual mode | Hugo

hard to track down without the source config and content layout

Short note: In Hugo < 0.123 you need to end your baseURL with a / (which is not the case in your example).

content=“Hugo 0.124.0”

TOML has NO indentations. Your defaultContentLanguageInSubdir is actually module.imports.defaultCOntentLangaugeInSubdir.

Move ALL items that are not below anything between [ something ] in your config above those sections.

And have a 5 minute readthrough of this page that explains these details quite nicely.

3 Likes

The page works perfectly if the hugo.toml looks like this. But only one domain is used, namely example.com. When switching to the German language, the link looks like this:

#english                   
example.com                                          
example.com/company/
example.com/imprint/

#german
example.com/de/
example.com/de/unternehmen/
example.com/de/impressum/

Because I have 2 domains, the result should look like this:

#english                   
example.com                                          
example.com/company/
example.com/imprint/

#german
example.de/
example.de/unternehmen/
example.de/impressum/

#baseURL = "https://example.com"
title = ""
languageCode = "en-US"
theme = "example"

enableRobotsTXT = true
relativeUrls = true
defaultContentLanguage = "de"

defaultContentLanguageInSubdir = true

[module]
	[[module.imports]]
		path = "github.com/gohugoio/hugo-mod-bootstrap-scss/v5"

[languages]
  [languages.en]
    baseURL = "https://example.com"
	  languageCode = "en-US"
    languageName = "English"
    contentDir = "content/en"
  [languages.de]
    baseURL = "https://example.de"
		languageCode = "de-DE"
    languageName = "Deutsch"
    contentDir = "content/de"

[markup]
	[markup.goldmark]
		[markup.goldmark.renderer]
    		unsafe = true

[sitemap]
  ChangeFreq = "weekly"
  Priority = ".5"

[privacy]
  [privacy.disqus]
    disable = true
  [privacy.googleAnalytics]
      disable = true
  [privacy.instagram]
      disable = true
  [privacy.twitter]
     disable = true
  [privacy.vimeo]
    disable = true
  [privacy.youtube]
    disable = true

You need to remove the comments from your baseURL settings in the languages sections.

The solution comes from @irkode. Thank you.


{{ range $.AllTranslations }}
<li><a class='dropdown-item {{ if eq .Language.Lang $.Language.Lang }} active {{ end }}' href='{{ .Permalink }}'
		hreflang="{{ $.Language.Lang }}"> {{ .Language.LanguageName }} </a>
</li>
{{ end }}

and ofc with the correction from @davidsneighbour in hugo.yaml to move settings to top

additionally setting a DefaultContentLanguage helps to bind the language to the correct baseURL

defaultContentLanguage = "de"
defaultContentLanguageInSubdir = true

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.