Files
blitzkit/packages/website/src/components/LocaleSwitcher.tsx
T

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-01-10 14:37:29 -06:00
import {
2025-01-16 14:10:28 -06:00
STRINGS,
2025-01-14 19:22:41 -06:00
SUPPORTED_LOCALE_FLAGS,
2025-09-27 15:38:37 -05:00
type BlitzKitStrings,
} from "@blitzkit/i18n";
import locales from "@blitzkit/i18n/locales.json";
import { Select } from "@radix-ui/themes";
import type { LocaleAcceptorProps } from "../hooks/useLocale";
import { BlitzKitTheme } from "./BlitzKitTheme";
2025-01-15 13:10:57 -06:00
export function LocaleSwitcherThemeWrapper({ locale }: LocaleAcceptorProps) {
return (
2025-09-27 15:38:37 -05:00
<BlitzKitTheme style={{ background: "transparent" }}>
2025-01-15 13:10:57 -06:00
<LocaleSwitcher locale={locale} />
</BlitzKitTheme>
);
}
2025-01-10 14:37:29 -06:00
export function LocaleSwitcher({ locale }: LocaleAcceptorProps) {
return (
2025-01-10 20:46:15 -06:00
<Select.Root
defaultValue={locale}
onValueChange={(locale) => {
2025-09-27 15:38:37 -05:00
localStorage.setItem("preferred-locale", locale);
2025-01-15 13:10:57 -06:00
let rawPath = window.location.pathname;
2025-09-27 15:38:37 -05:00
for (const supported of locales.supported) {
if (window.location.pathname.startsWith(`/${supported.locale}`)) {
rawPath = rawPath.replace(`/${supported.locale}`, "");
2025-01-15 13:10:57 -06:00
break;
}
}
window.location.pathname = `${
2025-09-27 15:38:37 -05:00
locale === locales.default ? "" : `/${locale}`
2025-01-15 13:10:57 -06:00
}${rawPath}`;
2025-01-10 20:46:15 -06:00
}}
>
2025-04-21 17:01:32 -05:00
<Select.Trigger variant="classic" />
2025-01-10 14:37:29 -06:00
2025-01-10 20:46:15 -06:00
<Select.Content>
2025-09-27 15:38:37 -05:00
{locales.supported.map(({ locale }) => (
2025-01-10 20:46:15 -06:00
<Select.Item key={locale} value={locale}>
2025-09-27 15:38:37 -05:00
{SUPPORTED_LOCALE_FLAGS[locale]}{" "}
{
STRINGS[locale].common.locales[
locale as keyof BlitzKitStrings["common"]["locales"]
]
}
2025-01-10 20:46:15 -06:00
</Select.Item>
))}
</Select.Content>
</Select.Root>
2025-01-10 14:37:29 -06:00
);
}