feat: make language-selector more robust
This commit is contained in:
parent
615e4466de
commit
0072eac5d2
|
|
@ -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></>);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue