feat: make language-selector more robust

This commit is contained in:
Dariusz Niemczyk 2025-02-15 00:35:32 +01:00
parent 615e4466de
commit 0072eac5d2
No known key found for this signature in database

View file

@ -2,17 +2,23 @@
import { Lang } from "@/i18n/locales"; import { Lang } from "@/i18n/locales";
import Link from "next/link"; import Link from "next/link";
import { useParams } from "next/navigation"; import { useParams, usePathname } from "next/navigation";
export const LanguageSelector = () => { export const LanguageSelector = () => {
const params = useParams<{ locale: Lang }>(); const params = useParams<{ locale: Lang }>();
const pathname = usePathname()
const replacements = {
'pl': 'en',
'en': 'pl',
}
const lang = params?.locale || 'pl'; const lang = params?.locale || 'pl';
const hash = globalThis?.window?.location?.hash || ''; const changedLang = pathname.replace(`/${lang}/`, `/${replacements[lang]}/`)
if (lang === 'pl') return (<> if (lang === 'pl') return (<>
<Link suppressHydrationWarning className="pt-1" href={`/en${hash}`}>🇬🇧</Link></>); <Link suppressHydrationWarning className="pt-1" href={changedLang}>🇬🇧</Link></>);
if (lang === 'en') return (<> if (lang === 'en') return (<>
<Link suppressHydrationWarning className="pt-1" href={`/pl${hash}`}>🇵🇱</Link></>); <Link suppressHydrationWarning className="pt-1" href={changedLang}>🇵🇱</Link></>);
}; };