'use client' import { useState } from 'react' import type { I18nConfig } from './types.js' import { useAlternateLinks } from './AlternateLinksContext.js' export type LanguageSwitcherClassNames = { item?: string itemActive?: string menu?: string root?: string trigger?: string } type LocaleItem = { code: string; label: string } /** * Language switcher that links to the CORRECT localized URL per language, * reading the current page's alternates from AlternateLinksProvider — so it * never 404s on differing slugs (/pl/uslugi vs /en/services). * * Unstyled by default (classNames, like CookieBanner) — NOT a fixed * light/dark variant, so it matches any project's design. A locale with no * known alternate falls back to its root (/{code}). * * // layout/header (client), inside AlternateLinksProvider * */ export function LanguageSwitcher({ classNames, config, currentLocale, renderLabel, }: { classNames?: LanguageSwitcherClassNames config: I18nConfig currentLocale: string /** Custom label per locale (e.g. flag + code). Defaults to the config label. */ renderLabel?: (locale: LocaleItem) => React.ReactNode }) { const alternates = useAlternateLinks() const [open, setOpen] = useState(false) const cn = classNames ?? {} const locales: LocaleItem[] = config.locales.map((l) => ({ code: l.code, label: l.label ?? l.code.toUpperCase(), })) const hrefFor = (code: string): string => alternates[code] ?? `/${code}` const current = locales.find((l) => l.code === currentLocale) return (
{open && ( )}
) }