feat: add language switcher with cookie persistence and browser detection

Changes:
- Root page.tsx now detects browser language (Accept-Language header)
  and cookie preference to redirect to correct locale (/sr or /en)
- Middleware handles old Serbian URLs (/products, /about, etc.) with
  301 redirects to /sr/* while respecting locale cookie
- Header component now includes language switcher dropdown with
  flag icons (Serbian and English)
- Language selection sets NEXT_LOCALE cookie and persists preference

Behavior:
- User visits / → redirects to /sr or /en based on cookie/browser
- User selects English from dropdown → cookie set, redirects to /en/*
- User visits /products with en cookie → /en/products (301)
- User visits /products with sr/no cookie → /sr/products (301)
This commit is contained in:
Unchained
2026-03-24 08:09:27 +02:00
parent 8244ba161b
commit a84647db6c
3 changed files with 107 additions and 29 deletions

View File

@@ -1,5 +1,18 @@
import { redirect } from "next/navigation";
import { cookies, headers } from "next/headers";
export default function RootPage() {
redirect("/sr");
}
export default async function RootPage() {
const headersList = await headers();
const cookieStore = await cookies();
const acceptLanguage = headersList.get("accept-language") || "";
const cookieLocale = cookieStore.get("NEXT_LOCALE")?.value;
let locale = "sr";
if (cookieLocale && ["sr", "en", "de", "fr"].includes(cookieLocale)) {
locale = cookieLocale;
} else if (acceptLanguage.includes("en")) {
locale = "en";
}
redirect(`/${locale}`);
}