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
+1
View File
@@ -19,6 +19,7 @@ const headerProps = {
nav: getMainNavigation('hu'),
homeHref: '/hu',
a11y: common.a11y,
languageLabel: 'Nyelv kiválasztása',
}
describe('Header', () => {
+8 -3
View File
@@ -7,14 +7,17 @@ import Link from 'next/link'
import Image from 'next/image'
import type { NavigationItem } from '@/types/site'
import { externalLinkProps, isExternalHref } from '@/lib/external-link'
import { LanguageSwitcher } from './LanguageSwitcher'
type HeaderProps = {
nav: NavigationItem[]
homeHref: string
a11y: { openMenu: string; closeMenu: string }
/** MITHOME-115: accessible name of the language switcher group. */
languageLabel: string
}
export default function Header({ nav, homeHref, a11y }: HeaderProps) {
export default function Header({ nav, homeHref, a11y, languageLabel }: HeaderProps) {
const [isMenuOpen, setIsMenuOpen] = useState(false)
const [isScrolled, setIsScrolled] = useState(false)
@@ -102,14 +105,16 @@ export default function Header({ nav, homeHref, a11y }: HeaderProps) {
</a>
))}
{/* Theme Toggle */}
<div className="ml-2 pl-2" style={{ borderLeft: `1px solid var(--color-border)` }}>
{/* Language switcher + Theme Toggle */}
<div className="ml-2 pl-2 flex items-center gap-1" style={{ borderLeft: `1px solid var(--color-border)` }}>
<LanguageSwitcher label={languageLabel} />
<ThemeToggle />
</div>
</div>
{/* Mobile menu button */}
<div className="md:hidden flex items-center gap-2">
<LanguageSwitcher label={languageLabel} />
<ThemeToggle />
<button
type="button"
@@ -0,0 +1,39 @@
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import { usePathname } from 'next/navigation'
import { LanguageSwitcher } from './LanguageSwitcher'
jest.mock('next/navigation', () => ({ usePathname: jest.fn() }))
const mockPath = (path: string) => (usePathname as jest.Mock).mockReturnValue(path)
describe('LanguageSwitcher (MITHOME-115)', () => {
it('should link both languages to the same page with translated slugs', () => {
mockPath('/hu/szolgaltatasok')
render(<LanguageSwitcher label="Nyelv kiválasztása" />)
expect(screen.getByRole('link', { name: 'Magyar' })).toHaveAttribute('href', '/hu/szolgaltatasok')
expect(screen.getByRole('link', { name: 'English' })).toHaveAttribute('href', '/en/services')
})
it('should mark the current language and label the group', () => {
mockPath('/en/about')
render(<LanguageSwitcher label="Choose language" />)
expect(screen.getByRole('group', { name: 'Choose language' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'English' })).toHaveAttribute('aria-current', 'true')
expect(screen.getByRole('link', { name: 'Magyar' })).not.toHaveAttribute('aria-current')
})
it('should declare the language of each link for assistive technology', () => {
mockPath('/hu')
render(<LanguageSwitcher label="Nyelv kiválasztása" />)
const english = screen.getByRole('link', { name: 'English' })
expect(english).toHaveAttribute('lang', 'en')
expect(english).toHaveAttribute('hrefLang', 'en')
expect(english).toHaveTextContent('EN')
})
it('should always offer English, even from an unknown page', () => {
mockPath('/hu/nincs-ilyen')
render(<LanguageSwitcher label="Nyelv kiválasztása" />)
expect(screen.getByRole('link', { name: 'English' })).toHaveAttribute('href', '/en')
})
})
+48
View File
@@ -0,0 +1,48 @@
'use client'
import { Fragment } from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { LOCALE_NAMES, languageLinks } from '@/lib/i18n'
/**
* MITHOME-115: compact "HU | EN" switcher. Each link keeps the current page
* in the other language (translated slug, src/lib/i18n.ts languageLinks).
*
* DECISION (user): English is always offered, even before a page's English
* translation is approved — the page then shows Hungarian text (MITHOME-113
* gate). No flags: a flag marks a country, not a language.
* WHY `lang`/`hrefLang` on the links: screen readers pronounce "English"
* correctly on a Hungarian page (WCAG 3.1.2).
*/
export function LanguageSwitcher({ label }: { label: string }) {
const links = languageLinks(usePathname() ?? '/')
return (
<div role="group" aria-label={label} className="flex items-center text-sm">
{links.map((link, index) => (
<Fragment key={link.locale}>
{index > 0 && (
<span aria-hidden="true" style={{ color: 'var(--color-border)' }}>
|
</span>
)}
<Link
href={link.href}
hrefLang={link.locale}
lang={link.locale}
aria-label={LOCALE_NAMES[link.locale].name}
aria-current={link.isActive ? 'true' : undefined}
className="px-1.5 py-1 rounded transition-colors duration-200 hover-scale"
style={{
color: link.isActive ? 'var(--color-primary-600)' : 'var(--color-foreground-muted)',
fontWeight: link.isActive ? 700 : 500,
}}
>
{LOCALE_NAMES[link.locale].code}
</Link>
</Fragment>
))}
</div>
)
}