lang.js: remember the language a switcher click picked

The switcher links were plain anchors: clicking one navigated but stored nothing, so
the auto-detect on the EN homepage bounced the visitor straight back. A de-DE browser
on /de/ that clicked EN landed on / and was replaced back to /de/ — the 'an explicit
choice always wins' promise in the plan and in llms.txt was only true for ?lang=.

A click now calls remember() with the link's own hreflang, so the choice is stored in
localStorage + the o4p_lang cookie and survives the next session. Selector tests
27 -> 31 (switcher click stores EN, sets the cookie, stores DE, chosen EN survives a
new session).
This commit is contained in:
oliver committed 2026-10-24 21:25:33 +00:00
1 parent 937dbd4dbf
commit 06beab403d
1 file changed
+2
+2
View File
@@ -33,6 +33,8 @@ render(c);
function render(current){var nav=document.createElement("nav"),i,a;nav.className="lang-switch";nav.setAttribute("aria-label","Language"); function render(current){var nav=document.createElement("nav"),i,a;nav.className="lang-switch";nav.setAttribute("aria-label","Language");
for(i=0;i<ORDER.length;i++){a=document.createElement("a");a.href=urlFor(ORDER[i]);a.textContent=ORDER[i].toUpperCase(); for(i=0;i<ORDER.length;i++){a=document.createElement("a");a.href=urlFor(ORDER[i]);a.textContent=ORDER[i].toUpperCase();
a.setAttribute("hreflang",ORDER[i]==="en"?"en-US":ORDER[i]);if(ORDER[i]===current)a.setAttribute("aria-current","true"); a.setAttribute("hreflang",ORDER[i]==="en"?"en-US":ORDER[i]);if(ORDER[i]===current)a.setAttribute("aria-current","true");
/* a click is an explicit choice: remember it, or the auto-detect bounces the visitor back */
a.onclick=function(){var l=String(this.getAttribute("hreflang")||"").toLowerCase();remember(l.indexOf("en")===0?"en":l)};
nav.appendChild(a);if(i<ORDER.length-1)nav.appendChild(document.createTextNode(" | "))} nav.appendChild(a);if(i<ORDER.length-1)nav.appendChild(document.createTextNode(" | "))}
var slot=null;try{slot=document.querySelector("[data-lang-switcher]")}catch(e){slot=null} var slot=null;try{slot=document.querySelector("[data-lang-switcher]")}catch(e){slot=null}
try{if(!document.getElementById("o4p-lang-style")){var s=document.createElement("style");s.id="o4p-lang-style"; try{if(!document.getElementById("o4p-lang-style")){var s=document.createElement("style");s.id="o4p-lang-style";