From d873b06c07fa6b7654434273ab972166462a0ea8 Mon Sep 17 00:00:00 2001 From: Keith Date: Sun, 22 Nov 2026 15:16:56 +0100 Subject: [PATCH] One-time browser-language auto-detect (default locale only): visitors whose navigator.languages match a locale that actually has pages get location.replace'd to that locale's URL once, stored in localStorage vl-lang; the switcher's manual pick overwrites the flag; crawlers (en-US) and untranslated locales unaffected --- src/layouts/Site.astro | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/layouts/Site.astro b/src/layouts/Site.astro index e0e556f..7782abb 100644 --- a/src/layouts/Site.astro +++ b/src/layouts/Site.astro @@ -131,6 +131,25 @@ const hasForm = isAudiencePage; // DOMContentLoaded, not at parse time (found live 2026-11-21: the // banner never dismissed because it did not exist yet on lookup). const KEY = "vl-consent"; + const LANG_KEY = "vl-lang"; + // One-time browser-language detection, default locale only: if the + // visitor's navigator.languages match one of our locales and they + // haven't chosen one (stored flag), hop to that locale's version of + // this page ONCE. Crawlers send en-US, so they never get redirected; + // hreflang/sitemap already handle indexing of every locale URL. + try { + if (${JSON.stringify(locale)} === ${JSON.stringify(DEFAULT_LOCALE)} && !localStorage.getItem(LANG_KEY)) { + const codes = ${JSON.stringify([...new Set(parsePages().map((p) => p.code))])}; + const want = (navigator.languages || [navigator.language || ""]).map((l) => String(l).slice(0, 2).toLowerCase()); + const hit = want.find((w) => codes.includes(w) && w !== ${JSON.stringify(DEFAULT_LOCALE)}); + if (hit) { + localStorage.setItem(LANG_KEY, hit); + const prefix = "/" + hit; + const rest = ${JSON.stringify(pageId)} === "" ? "/" : "/" + ${JSON.stringify(pageId)} + "/"; + location.replace(${JSON.stringify(baseUrl)} + prefix + rest); + } + } + } catch {} const STATS_URL = ${JSON.stringify(site.stats_script_url)}; const STATS_ID = ${JSON.stringify(site.stats_website_id)}; const injectStats = () => { @@ -207,7 +226,7 @@ const hasForm = isAudiencePage;