mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-28 04:59:50 +01:00
feat: auto prefixor
This commit is contained in:
1 parent
ffe58de179
commit
260d819c77
6 files changed
+65
-34
No files matched your search
@@ -26,11 +26,12 @@
|
|||||||
"locales": {
|
"locales": {
|
||||||
"en": "English",
|
"en": "English",
|
||||||
"es": "Spanish",
|
"es": "Spanish",
|
||||||
"hr": "Croatian",
|
|
||||||
"pt": "Portuguese",
|
"pt": "Portuguese",
|
||||||
"ru": "Russian",
|
"ru": "Russian",
|
||||||
"uk": "Ukrainian",
|
"uk": "Ukrainian",
|
||||||
"zh": "Chinese"
|
"zh": "Chinese",
|
||||||
|
"ja": "Japanese",
|
||||||
|
"fr": "French"
|
||||||
},
|
},
|
||||||
|
|
||||||
"wn8_percentile": {
|
"wn8_percentile": {
|
||||||
|
|||||||
@@ -23,3 +23,14 @@ export const SUPPORTED_LOCALE_BLITZ_MAP: Record<SupportedLocale, string> = {
|
|||||||
uk: 'uk',
|
uk: 'uk',
|
||||||
zh: 'zh-Hans',
|
zh: 'zh-Hans',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SUPPORTED_LOCALE_FLAGS: Record<SupportedLocale, string> = {
|
||||||
|
en: '🇺🇸',
|
||||||
|
es: '🇪🇸',
|
||||||
|
fr: '🇫🇷',
|
||||||
|
ja: '🇯🇵',
|
||||||
|
pt: '🇵🇹',
|
||||||
|
ru: '🇷🇺',
|
||||||
|
uk: '🇺🇦',
|
||||||
|
zh: '🇨🇳',
|
||||||
|
};
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
import { Link, type LinkProps } from '@radix-ui/themes';
|
import { Link, type LinkProps } from '@radix-ui/themes';
|
||||||
import { getBestLocale } from '../core/i18n/getBestLocale';
|
|
||||||
import type { LocaleAcceptorProps } from '../hooks/useLocale';
|
import type { LocaleAcceptorProps } from '../hooks/useLocale';
|
||||||
|
|
||||||
type LinkI18nProps = LinkProps & LocaleAcceptorProps;
|
type LinkI18nProps = LinkProps & LocaleAcceptorProps;
|
||||||
|
|
||||||
export function LinkI18n({ locale, href, ...props }: LinkI18nProps) {
|
export function LinkI18n({ locale, href, ...props }: LinkI18nProps) {
|
||||||
const bestLocale = getBestLocale();
|
const prefix = locale === undefined ? '' : `/${locale}`;
|
||||||
const resolvedLocale = locale ?? bestLocale;
|
|
||||||
const prefix = resolvedLocale === undefined ? '' : `/${resolvedLocale}`;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Link href={href?.startsWith('/') ? `${prefix}${href}` : href} {...props} />
|
<Link href={href?.startsWith('/') ? `${prefix}${href}` : href} {...props} />
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
DEFAULT_LOCALE,
|
||||||
|
localizedStrings,
|
||||||
|
SUPPORTED_LOCALE_FLAGS,
|
||||||
|
SUPPORTED_LOCALES,
|
||||||
|
} from '@blitzkit/core';
|
||||||
|
import { GlobeIcon } from '@radix-ui/react-icons';
|
||||||
|
import { Flex, Select, Text } from '@radix-ui/themes';
|
||||||
|
import type { LocaleAcceptorProps } from '../hooks/useLocale';
|
||||||
|
import { BlitzKitTheme } from './BlitzKitTheme';
|
||||||
|
|
||||||
|
export function LocaleSwitcher({ locale }: LocaleAcceptorProps) {
|
||||||
|
return (
|
||||||
|
<BlitzKitTheme>
|
||||||
|
<Flex justify="center" mt="4" py="6" align="center" gap="3">
|
||||||
|
<Text size="5" trim="end" color="gray">
|
||||||
|
<GlobeIcon width="1em" height="1em" />
|
||||||
|
</Text>
|
||||||
|
|
||||||
|
<Select.Root
|
||||||
|
defaultValue={locale}
|
||||||
|
onValueChange={(locale) => {
|
||||||
|
localStorage.setItem('preferred-locale', locale);
|
||||||
|
window.location.pathname = `/${locale === DEFAULT_LOCALE ? '' : locale}`;
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Select.Trigger />
|
||||||
|
|
||||||
|
<Select.Content>
|
||||||
|
{SUPPORTED_LOCALES.map((locale) => (
|
||||||
|
<Select.Item key={locale} value={locale}>
|
||||||
|
{SUPPORTED_LOCALE_FLAGS[locale]}{' '}
|
||||||
|
{localizedStrings[locale].common.locales[locale]}
|
||||||
|
</Select.Item>
|
||||||
|
))}
|
||||||
|
</Select.Content>
|
||||||
|
</Select.Root>
|
||||||
|
</Flex>
|
||||||
|
</BlitzKitTheme>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from '@blitzkit/core';
|
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from '@blitzkit/core';
|
||||||
|
|
||||||
export function getBestLocale() {
|
export function getBestLocale() {
|
||||||
const desiredLocale = navigator.language.split('-')[0];
|
const desiredLocale =
|
||||||
const isSupported = SUPPORTED_LOCALES.includes(desiredLocale);
|
localStorage.getItem('preferred-locale') ??
|
||||||
|
navigator.language.split('-')[0];
|
||||||
|
const isSupported = (SUPPORTED_LOCALES as unknown as string[]).includes(
|
||||||
|
desiredLocale,
|
||||||
|
);
|
||||||
const isDefault = desiredLocale === DEFAULT_LOCALE;
|
const isDefault = desiredLocale === DEFAULT_LOCALE;
|
||||||
|
|
||||||
if (!isSupported || isDefault) return undefined;
|
if (!isSupported || isDefault) return undefined;
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
import {
|
import {
|
||||||
assertSecret,
|
assertSecret,
|
||||||
DEFAULT_LOCALE,
|
DEFAULT_LOCALE,
|
||||||
localizedStrings,
|
|
||||||
SUPPORTED_LOCALES,
|
SUPPORTED_LOCALES,
|
||||||
} from '@blitzkit/core';
|
} from '@blitzkit/core';
|
||||||
import { Box, Flex, Heading, Link, Separator } from '@radix-ui/themes';
|
import { Box, Flex, Heading } from '@radix-ui/themes';
|
||||||
import type { GetStaticPaths } from 'astro';
|
import type { GetStaticPaths } from 'astro';
|
||||||
import { AesonPlug } from '../../components/AesonPlug';
|
import { AesonPlug } from '../../components/AesonPlug';
|
||||||
import HomeHero from '../../components/HomeHero.astro';
|
import HomeHero from '../../components/HomeHero.astro';
|
||||||
import { HomePageHotTanks } from '../../components/HomePageHotTanks';
|
import { HomePageHotTanks } from '../../components/HomePageHotTanks';
|
||||||
|
import { LocaleSwitcher } from '../../components/LocaleSwitcher';
|
||||||
import { Plugs } from '../../components/Plugs';
|
import { Plugs } from '../../components/Plugs';
|
||||||
import ToolCard from '../../components/ToolCard.astro';
|
import ToolCard from '../../components/ToolCard.astro';
|
||||||
import {
|
import {
|
||||||
@@ -42,30 +42,7 @@ export const getStaticPaths = (() => {
|
|||||||
<BaseLayout>
|
<BaseLayout>
|
||||||
<HomeHero />
|
<HomeHero />
|
||||||
|
|
||||||
<Flex justify="center" gap="3" pt="6" pb="4" align="center">
|
<LocaleSwitcher {locale} client:load />
|
||||||
{
|
|
||||||
SUPPORTED_LOCALES.map((thisLocale, index) => {
|
|
||||||
const selected =
|
|
||||||
thisLocale === locale ||
|
|
||||||
(locale === undefined && thisLocale === DEFAULT_LOCALE);
|
|
||||||
const localeNames = localizedStrings[thisLocale].common.locales;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{index > 0 && <Separator orientation="vertical" size="1" />}
|
|
||||||
|
|
||||||
<Link
|
|
||||||
color="gray"
|
|
||||||
highContrast={selected}
|
|
||||||
href={`/${thisLocale === DEFAULT_LOCALE ? '' : thisLocale}`}
|
|
||||||
>
|
|
||||||
{localeNames[thisLocale as keyof typeof localeNames]}
|
|
||||||
</Link>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</Flex>
|
|
||||||
|
|
||||||
<Plugs client:idle />
|
<Plugs client:idle />
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user