driven breakdown tables, with frontend/cards/dialog updated accordingly (database/ migrations/2025_10_17_000001_add_description_table_to_packages.php, database/ migrations/2025_10_17_000002_add_translation_columns_to_packages.php, database/seeders/PackageSeeder.php, app/ Http/Controllers/MarketingController.php, resources/js/pages/marketing/Packages.tsx). Filament Package resource gains locale tabs, markdown editor, numeric/toggle inputs, and simplified feature management (app/Filament/Resources/PackageResource.php, app/Filament/Resources/PackageResource/Pages/ CreatePackage.php, .../EditPackage.php). Legal pages now render markdown-backed content inside the main layout via a new controller/view route setup and updated footer links (app/Http/Controllers/LegalPageController.php, routes/web.php, resources/views/partials/ footer.blade.php, resources/js/pages/legal/Show.tsx, remove old static pages). Translation files and shared assets updated to cover new marketing/legal strings and styling tweaks (public/ lang/*/marketing.json, resources/lang/*/marketing.php, resources/css/app.css, resources/js/admin/components/ LanguageSwitcher.tsx).
110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import React from 'react';
|
|
import { Check, Languages } from 'lucide-react';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from '@/components/ui/dropdown-menu';
|
|
|
|
import i18n from '../i18n';
|
|
|
|
type SupportedLocale = 'de' | 'en';
|
|
|
|
const SUPPORTED_LANGUAGES: Array<{ code: SupportedLocale; labelKey: string }> = [
|
|
{ code: 'de', labelKey: 'language.de' },
|
|
{ code: 'en', labelKey: 'language.en' },
|
|
];
|
|
|
|
function getCsrfToken(): string {
|
|
return document.querySelector<HTMLMetaElement>('meta[name=\"csrf-token\"]')?.content ?? '';
|
|
}
|
|
|
|
async function persistLocale(locale: SupportedLocale): Promise<void> {
|
|
const response = await fetch('/set-locale', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': getCsrfToken(),
|
|
Accept: 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
body: JSON.stringify({ locale }),
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`locale update failed with status ${response.status}`);
|
|
}
|
|
}
|
|
|
|
export function LanguageSwitcher() {
|
|
const { t } = useTranslation('common');
|
|
const [pendingLocale, setPendingLocale] = React.useState<SupportedLocale | null>(null);
|
|
|
|
const currentLocale = (i18n.language || document.documentElement.lang || 'de') as SupportedLocale;
|
|
|
|
const changeLanguage = React.useCallback(
|
|
async (locale: SupportedLocale) => {
|
|
if (locale === currentLocale || pendingLocale) {
|
|
return;
|
|
}
|
|
|
|
setPendingLocale(locale);
|
|
try {
|
|
await persistLocale(locale);
|
|
await i18n.changeLanguage(locale);
|
|
document.documentElement.setAttribute('lang', locale);
|
|
} catch (error) {
|
|
if (import.meta.env.DEV) {
|
|
|
|
console.error('Failed to switch language', error);
|
|
}
|
|
} finally {
|
|
setPendingLocale(null);
|
|
}
|
|
},
|
|
[currentLocale, pendingLocale]
|
|
);
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
className="border-brand-rose-soft text-brand-rose hover:bg-brand-rose-soft/40"
|
|
aria-label={t('app.languageSwitch')}
|
|
>
|
|
<Languages className="mr-2 h-4 w-4" />
|
|
<span className="hidden sm:inline">{t('app.languageSwitch')}</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
{SUPPORTED_LANGUAGES.map(({ code, labelKey }) => {
|
|
const isActive = currentLocale === code;
|
|
const isPending = pendingLocale === code;
|
|
|
|
return (
|
|
<DropdownMenuItem
|
|
key={code}
|
|
onSelect={(event) => {
|
|
event.preventDefault();
|
|
changeLanguage(code);
|
|
}}
|
|
className="flex items-center justify-between gap-3"
|
|
disabled={isPending}
|
|
>
|
|
<span>{t(labelKey)}</span>
|
|
{(isActive || isPending) && <Check className="h-4 w-4 text-brand-rose" />}
|
|
</DropdownMenuItem>
|
|
);
|
|
})}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
}
|