From 624b6acac87fdeb47ba00ee60530bfdb4d56a796 Mon Sep 17 00:00:00 2001 From: juanatsap Date: Sun, 9 Nov 2025 15:10:13 +0000 Subject: [PATCH] Fix language switcher to maintain clean URLs and dynamic logo links Changes: - Logo and title links now start with clean URLs (href="/") - Track if URL originally had lang parameter (urlHadLangParam variable) - If URL had lang param: update URL when switching languages, update logo/title links dynamically - If URL was clean: keep it clean, don't add lang to URL, keep logo/title links clean - Logo/title links now update dynamically when language changes (no more stale language issue) - Initialize logo/title links properly on page load based on URL state This fixes both issues: 1. Logo clicking with wrong language after language switch 2. Clean URLs stay clean unless explicitly using ?lang= parameter --- templates/index.html | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/templates/index.html b/templates/index.html index 92e8534..be59d9f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -99,7 +99,7 @@
- + @@ -108,7 +108,7 @@ - + CV JAMR - {{.CurrentYear}} @@ -375,6 +375,9 @@ } }); + // Track if URL originally had lang parameter + const urlHadLangParam = new URLSearchParams(window.location.search).has('lang'); + function selectLanguage(lang) { // Update button states document.querySelectorAll('.language-selector .selector-btn').forEach(btn => { @@ -389,10 +392,20 @@ indicator: '#loading' }); - // Update URL + // Update URL only if it originally had lang parameter const url = new URL(window.location); - url.searchParams.set('lang', lang); - window.history.pushState({}, '', url); + if (urlHadLangParam) { + url.searchParams.set('lang', lang); + window.history.pushState({}, '', url); + + // Update logo and title links to include current language + document.getElementById('logoLink').href = `/?lang=${lang}`; + document.getElementById('titleLink').href = `/?lang=${lang}`; + } else { + // Keep URL clean, but update logo/title links to stay clean too + document.getElementById('logoLink').href = '/'; + document.getElementById('titleLink').href = '/'; + } // Update html lang attribute document.documentElement.lang = lang; @@ -480,6 +493,13 @@ document.addEventListener('DOMContentLoaded', function() { const paper = document.querySelector('.cv-paper'); + // Initialize logo/title links based on whether URL has lang parameter + if (urlHadLangParam) { + const currentLang = new URLSearchParams(window.location.search).get('lang') || 'en'; + document.getElementById('logoLink').href = `/?lang=${currentLang}`; + document.getElementById('titleLink').href = `/?lang=${currentLang}`; + } + // Restore CV length preference const savedLength = localStorage.getItem('cv-length') || 'short'; if (savedLength === 'long') {