-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageDetector.astro
More file actions
55 lines (46 loc) · 1.9 KB
/
Copy pathLanguageDetector.astro
File metadata and controls
55 lines (46 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
// src/components/LanguageDetector.astro
// Client-side browser language detection with SEO-safe redirection.
// Only performs automatic detection on the home page to avoid breaking
// deep-linked / indexed pages. Manual preference (via header toggle)
// is respected on ALL pages.
import type { Lang } from '@/lib/i18n';
interface Props {
lang: Lang;
alternateUrl?: string;
}
const { lang, alternateUrl } = Astro.props;
---
<script
define:vars={{ pageLang: lang, alternateUrl: alternateUrl ?? null }}
>
(function detectLanguage() {
const PREFERRED_LANG_KEY = 'preferred_lang';
const REDIRECTED_KEY = 'lang_redirected';
// Never redirect if we already redirected this session (prevents loops)
if (sessionStorage.getItem(REDIRECTED_KEY)) return;
const storedPref = localStorage.getItem(PREFERRED_LANG_KEY);
const isHomePage =
window.location.pathname === '/' ||
window.location.pathname === '/es/' ||
window.location.pathname === '/es';
/** @type {'en' | 'es' | null} */
let targetLang = null;
if (storedPref === 'en' || storedPref === 'es') {
// User has explicitly chosen a language before — respect it everywhere
targetLang = storedPref;
} else if (isHomePage) {
// First visit, no preference saved — detect from browser, but ONLY on
// the home page to preserve SEO for deep-linked pages.
const browserLang = navigator.language || navigator.userLanguage || 'en';
targetLang = browserLang.startsWith('es') ? 'es' : 'en';
// Persist the detected preference so subsequent pages are consistent
localStorage.setItem(PREFERRED_LANG_KEY, targetLang);
}
// If we have a target and it differs from the current page, redirect
if (targetLang && targetLang !== pageLang && alternateUrl) {
sessionStorage.setItem(REDIRECTED_KEY, '1');
window.location.replace(alternateUrl);
}
})();
</script>