Files
ipal-kit/src/modules/i18n/LanguageSwitcher.tsx
T
Radosław SmolińskiandClaude Opus 5.5 4ec27d9ef0 Built-in statistics (cookieless, consent-gated visitor history); per-page meta descriptions, NoSnippet
Stats: ipalKit({ stats: true }) adds the stats-visits collection, POST /api/ipal/hit, the
/admin/stats view (30-day visits chart, top/entry pages, referrers, devices), a dashboard card
and a nav link. <PageStats> (or <SiteProviders stats>) sends one beacon per page view. Without
consent nothing is stored on the device and no IP is kept: page views are joined by a hash with
an in-memory salt replaced daily. With analytics consent an ipal_vid cookie links visits across
days (server re-checks the consent cookie and policy version; withdrawal deletes the cookie).
Bots, logged-in panel users and prefetches aren't counted. Retention 90 days.
@payloadcms/next added to peerDependencies (DefaultTemplate). Docs: stats.md with privacy text.

Also in this release: meta.description auto-filled from page blocks, <NoSnippet>,
MetaDescriptionCheck in the SEO tab, data-nosnippet on banner/language switcher, GA4 CSP
wildcards, consent version passed to Analytics' default consent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-21 15:10:55 +02:00

95 lines
2.8 KiB
TypeScript

'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
* <LanguageSwitcher
* config={i18nConfig}
* currentLocale={locale}
* classNames={{ root: '...', trigger: '...', menu: '...', item: '...' }}
* />
*/
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 (
<div className={cn.root} data-ipal-langswitcher data-nosnippet>
<button
aria-expanded={open}
aria-haspopup="true"
className={cn.trigger}
onClick={() => setOpen((o) => !o)}
type="button"
>
{current ? (renderLabel ? renderLabel(current) : current.label) : currentLocale}
</button>
{open && (
<ul className={cn.menu} role="menu">
{locales.map((loc) => {
const active = loc.code === currentLocale
return (
<li key={loc.code} role="none">
<a
aria-current={active ? 'true' : undefined}
className={`${cn.item ?? ''} ${active ? (cn.itemActive ?? '') : ''}`.trim()}
href={hrefFor(loc.code)}
hrefLang={loc.code}
onClick={() => setOpen(false)}
role="menuitem"
>
{renderLabel ? renderLabel(loc) : loc.label}
</a>
</li>
)
})}
</ul>
)}
</div>
)
}