feat(web): HU | EN language switcher in the header (MITHOME-115)

A compact "HU | EN" switcher next to the theme toggle (desktop) and the
menu button (mobile). Each link opens the current page in the other
language with its translated slug (/hu/szolgaltatasok <-> /en/services),
unknown pages fall back to the language's home. English is always
offered (user decision), even before a page's English is approved.

Links carry lang/hrefLang, the active one aria-current, and the group a
localized accessible name. No flags: a flag marks a country, not a
language. Unit and component tests plus staging smoke SMOKE-08.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 12:22:11 +02:00
co-authored by Claude Opus 5.5
parent 8687ae28a2
commit 2bb9f122d5
9 changed files with 184 additions and 4 deletions
+34
View File
@@ -0,0 +1,34 @@
import { languageLinks, parseLocalePath } from './i18n'
describe('parseLocalePath (MITHOME-115)', () => {
it.each([
['/hu/rolunk', { locale: 'hu', slug: 'rolunk' }],
['/en', { locale: 'en', slug: undefined }],
['/en/services/', { locale: 'en', slug: 'services' }],
['/', { locale: 'hu' }],
['/admin', { locale: 'hu' }],
])('should parse %s', (path, expected) => {
expect(parseLocalePath(path)).toEqual(expected)
})
})
describe('languageLinks (MITHOME-115)', () => {
it('should keep the current page with its translated slug', () => {
expect(languageLinks('/hu/szolgaltatasok')).toEqual([
{ locale: 'hu', href: '/hu/szolgaltatasok', isActive: true },
{ locale: 'en', href: '/en/services', isActive: false },
])
expect(languageLinks('/en/privacy-policy').map((l) => l.href)).toEqual(['/hu/adatvedelem', '/en/privacy-policy'])
})
it('should link the home pages from a home page', () => {
expect(languageLinks('/en').map((l) => [l.href, l.isActive])).toEqual([
['/hu', false],
['/en', true],
])
})
it('should fall back to the other language home for an unknown page', () => {
expect(languageLinks('/hu/nincs-ilyen').map((l) => l.href)).toEqual(['/hu', '/en'])
})
})
+32
View File
@@ -50,3 +50,35 @@ export function switchLocalePath(currentLocale: Locale, targetLocale: Locale, sl
const key = resolvePageKey(currentLocale, slug)
return localePath(targetLocale, key)
}
/**
* MITHOME-115: nyelvnevek a nyelvválasztóhoz. WHY statikus és nem Payload-
* tartalom: a nyelv kódja és saját nyelvű neve (endonim) nem fordítandó
* szöveg — ugyanaz minden nyelvű oldalon.
*/
export const LOCALE_NAMES: Record<Locale, { code: string; name: string }> = {
hu: { code: 'HU', name: 'Magyar' },
en: { code: 'EN', name: 'English' },
}
/** A böngésző útvonalából a nyelv és a szlög (`/hu/rolunk` → hu, rolunk). */
export function parseLocalePath(pathname: string): { locale: Locale; slug?: string } {
const [first, second] = pathname.split('/').filter(Boolean)
return first && isLocale(first) ? { locale: first, slug: second } : { locale: DEFAULT_LOCALE }
}
export type LanguageLink = { locale: Locale; href: string; isActive: boolean }
/**
* MITHOME-115: a nyelvválasztó linkjei — minden nyelvre ugyanaz az oldal a
* lefordított szlöggel (/hu/szolgaltatasok ↔ /en/services); ismeretlen
* oldalnál az adott nyelv főoldala.
*/
export function languageLinks(pathname: string): LanguageLink[] {
const { locale, slug } = parseLocalePath(pathname)
return LOCALES.map((target) => ({
locale: target,
href: switchLocalePath(locale, target, slug),
isActive: target === locale,
}))
}