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:
co-authored by
Claude Opus 5.5
parent
8687ae28a2
commit
2bb9f122d5
@@ -75,6 +75,16 @@ test('SMOKE-07: external links on the homepage open in a new tab', async ({ page
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// MITHOME-115: the language switcher keeps the current page (translated slug).
|
||||||
|
test('SMOKE-08: language switcher opens the same page in the other language', async ({ page }) => {
|
||||||
|
await page.goto('/hu/szolgaltatasok')
|
||||||
|
const english = page.getByRole('group', { name: 'Nyelv kiválasztása' }).first().getByRole('link', { name: 'English' })
|
||||||
|
await expect(english).toHaveAttribute('href', '/en/services')
|
||||||
|
await english.click()
|
||||||
|
await expect(page).toHaveURL(/\/en\/services$/)
|
||||||
|
await expect(page.getByRole('group', { name: 'Choose language' }).first().getByRole('link', { name: 'Magyar' })).toHaveAttribute('href', '/hu/szolgaltatasok')
|
||||||
|
})
|
||||||
|
|
||||||
test('SMOKE-05: empty contact form is blocked client-side without API submission', async ({ page }) => {
|
test('SMOKE-05: empty contact form is blocked client-side without API submission', async ({ page }) => {
|
||||||
let contactPostCount = 0
|
let contactPostCount = 0
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import Header from '../../../components/Header'
|
|||||||
import Footer from '../../../components/Footer'
|
import Footer from '../../../components/Footer'
|
||||||
import { isLocale, localePath, type Locale } from '@/lib/i18n'
|
import { isLocale, localePath, type Locale } from '@/lib/i18n'
|
||||||
import { getCommonContent, getHomeContent } from '@/lib/payload-content'
|
import { getCommonContent, getHomeContent } from '@/lib/payload-content'
|
||||||
import { siteConfig, getMainNavigation, getFooterNavigation, getFooterLegalLinks, getSiteDescription } from '@/config/site'
|
import { siteConfig, getMainNavigation, getFooterNavigation, getFooterLegalLinks, getSiteDescription, getLanguageSwitcherLabel } from '@/config/site'
|
||||||
|
|
||||||
// WHY force-dynamic (MITHOME-97): ezek az oldalak a Payload Local API-t hívják
|
// WHY force-dynamic (MITHOME-97): ezek az oldalak a Payload Local API-t hívják
|
||||||
// (élő MongoDB-olvasás). generateStaticParams + SSG mellett a build időben
|
// (élő MongoDB-olvasás). generateStaticParams + SSG mellett a build időben
|
||||||
@@ -43,6 +43,7 @@ export default async function LocaleLayout({
|
|||||||
nav={getMainNavigation(locale)}
|
nav={getMainNavigation(locale)}
|
||||||
homeHref={localePath(locale)}
|
homeHref={localePath(locale)}
|
||||||
a11y={common.a11y}
|
a11y={common.a11y}
|
||||||
|
languageLabel={getLanguageSwitcherLabel(locale)}
|
||||||
/>
|
/>
|
||||||
<main className="flex-1">
|
<main className="flex-1">
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ const headerProps = {
|
|||||||
nav: getMainNavigation('hu'),
|
nav: getMainNavigation('hu'),
|
||||||
homeHref: '/hu',
|
homeHref: '/hu',
|
||||||
a11y: common.a11y,
|
a11y: common.a11y,
|
||||||
|
languageLabel: 'Nyelv kiválasztása',
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Header', () => {
|
describe('Header', () => {
|
||||||
|
|||||||
@@ -7,14 +7,17 @@ import Link from 'next/link'
|
|||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
import type { NavigationItem } from '@/types/site'
|
import type { NavigationItem } from '@/types/site'
|
||||||
import { externalLinkProps, isExternalHref } from '@/lib/external-link'
|
import { externalLinkProps, isExternalHref } from '@/lib/external-link'
|
||||||
|
import { LanguageSwitcher } from './LanguageSwitcher'
|
||||||
|
|
||||||
type HeaderProps = {
|
type HeaderProps = {
|
||||||
nav: NavigationItem[]
|
nav: NavigationItem[]
|
||||||
homeHref: string
|
homeHref: string
|
||||||
a11y: { openMenu: string; closeMenu: 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 [isMenuOpen, setIsMenuOpen] = useState(false)
|
||||||
const [isScrolled, setIsScrolled] = useState(false)
|
const [isScrolled, setIsScrolled] = useState(false)
|
||||||
|
|
||||||
@@ -102,14 +105,16 @@ export default function Header({ nav, homeHref, a11y }: HeaderProps) {
|
|||||||
</a>
|
</a>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
{/* Theme Toggle */}
|
{/* Language switcher + Theme Toggle */}
|
||||||
<div className="ml-2 pl-2" style={{ borderLeft: `1px solid var(--color-border)` }}>
|
<div className="ml-2 pl-2 flex items-center gap-1" style={{ borderLeft: `1px solid var(--color-border)` }}>
|
||||||
|
<LanguageSwitcher label={languageLabel} />
|
||||||
<ThemeToggle />
|
<ThemeToggle />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Mobile menu button */}
|
{/* Mobile menu button */}
|
||||||
<div className="md:hidden flex items-center gap-2">
|
<div className="md:hidden flex items-center gap-2">
|
||||||
|
<LanguageSwitcher label={languageLabel} />
|
||||||
<ThemeToggle />
|
<ThemeToggle />
|
||||||
<button
|
<button
|
||||||
type="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')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -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>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -42,6 +42,16 @@ const NAV_LABELS: Record<Locale, { home: string; about: string; services: string
|
|||||||
en: { home: 'Home', about: 'About', services: 'Services', contact: 'Contact', privacy: 'Privacy Policy', terms: 'Terms of Service' },
|
en: { home: 'Home', about: 'About', services: 'Services', contact: 'Contact', privacy: 'Privacy Policy', terms: 'Terms of Service' },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** MITHOME-115: a nyelvválasztó csoportjának akadálymentes címkéje. */
|
||||||
|
const LANGUAGE_SWITCHER_LABELS: Record<Locale, string> = {
|
||||||
|
hu: 'Nyelv kiválasztása',
|
||||||
|
en: 'Choose language',
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getLanguageSwitcherLabel(locale: Locale): string {
|
||||||
|
return LANGUAGE_SWITCHER_LABELS[locale]
|
||||||
|
}
|
||||||
|
|
||||||
export function getMainNavigation(locale: Locale): NavigationItem[] {
|
export function getMainNavigation(locale: Locale): NavigationItem[] {
|
||||||
const t = NAV_LABELS[locale]
|
const t = NAV_LABELS[locale]
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -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'])
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -50,3 +50,35 @@ export function switchLocalePath(currentLocale: Locale, targetLocale: Locale, sl
|
|||||||
const key = resolvePageKey(currentLocale, slug)
|
const key = resolvePageKey(currentLocale, slug)
|
||||||
return localePath(targetLocale, key)
|
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,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user