<div class="text-sm text-text-dim mb-3">{{ i18n "language" }}</div>
<div class="flex gap-2">
{{ $currentLang := .Page.Language }}
+ {{ $currentPath := .RelPermalink }}
{{ range .Site.Languages }}
{{ $langCode := .Lang }}
{{ $langName := .LanguageName }}
{{ $current := eq $langCode $currentLang }}
- {{ $prefix := cond (eq $langCode "en") "" (printf "/%s" $langCode) }}
+ <!-- Build the translated URL by replacing language prefix -->
+ {{ $url := $currentPath }}
+ {{ if eq $langCode "en" }}
+ {{ if hasPrefix $currentPath "/it/" }}
+ {{ $url = strings.TrimPrefix "/it" $currentPath }}
+ {{ end }}
+ {{ else }}
+ {{ if not (hasPrefix $currentPath "/it/") }}
+ {{ $url = printf "/it%s" $currentPath }}
+ {{ end }}
+ {{ end }}
<a
data-lang-switch="{{ $langCode }}"
- href="{{ $prefix }}/"
+ href="{{ $url }}"
class="flex-1 py-2 px-3 text-center rounded transition-colors {{ if $current }}bg-accent text-white{{ else }}bg-surface hover:bg-surface/80{{ end }}"
>
{{ $langName }}
<!-- Language switcher (desktop) -->
<div class="hidden md:flex gap-2">
{{ $currentLang := .Page.Language }}
+ {{ $currentPath := .RelPermalink }}
{{ range .Site.Languages }}
{{ $langCode := .Lang }}
{{ $langName := .LanguageName }}
{{ $current := eq $langCode $currentLang }}
- {{ $prefix := cond (eq $langCode "en") "" (printf "/%s" $langCode) }}
+ <!-- Build the translated URL by replacing language prefix -->
+ {{ $url := $currentPath }}
+ {{ if eq $langCode "en" }}
+ {{ if hasPrefix $currentPath "/it/" }}
+ {{ $url = strings.TrimPrefix "/it" $currentPath }}
+ {{ end }}
+ {{ else }}
+ {{ if not (hasPrefix $currentPath "/it/") }}
+ {{ $url = printf "/it%s" $currentPath }}
+ {{ end }}
+ {{ end }}
<a
- href="{{ $prefix }}/"
+ href="{{ $url }}"
class="px-2 py-1 text-sm rounded transition-colors {{ if $current }}bg-accent text-white{{ else }}hover:bg-surface{{ end }}"
>
{{ $langName }}