]> danix's work - danix.xyz-2.git/commitdiff
Fix language switcher to preserve current page path
authorDanilo M. <redacted>
Wed, 15 Apr 2026 15:33:26 +0000 (17:33 +0200)
committerDanilo M. <redacted>
Wed, 15 Apr 2026 15:33:26 +0000 (17:33 +0200)
- Update language switcher to translate current page path instead of redirecting to root
- When switching languages, preserve the page you're on (e.g., /articles/ → /it/articles/)
- Works for both desktop header and mobile hamburger menu
- Uses Hugo's string functions to manipulate URL paths

Co-Authored-By: Claude Haiku 4.5 <redacted>
themes/danix-xyz-hacker/layouts/partials/hamburger-menu.html
themes/danix-xyz-hacker/layouts/partials/header.html

index c22981be3791e3f5dd404370029f229ab34d20d9..ee80d208a3623f362fd9ee7108333095ee8dbc63 100644 (file)
       <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 }}
index 3afe9684d9ff4d4f034fa18702a288fbd9a43726..31467754ea5a1366b9ec464c29986821477622ba 100644 (file)
       <!-- 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 }}