Implementiere i18n für Frontend, Admin und Datenbank

This commit is contained in:
Hördle Bot
2025-11-28 15:36:06 +01:00
parent 9df9a808bf
commit 771d0d06f3
37 changed files with 3717 additions and 560 deletions

View File

@@ -0,0 +1,134 @@
import Game from '@/components/Game';
import NewsSection from '@/components/NewsSection';
import { getOrCreateDailyPuzzle } from '@/lib/dailyPuzzle';
import { Link } from '@/lib/navigation';
import { PrismaClient } from '@prisma/client';
import { notFound } from 'next/navigation';
import { getLocalizedValue } from '@/lib/i18n';
import { getTranslations } from 'next-intl/server';
export const dynamic = 'force-dynamic';
const prisma = new PrismaClient();
interface PageProps {
params: Promise<{ locale: string; genre: string }>;
}
export default async function GenrePage({ params }: PageProps) {
const { locale, genre } = await params;
const decodedGenre = decodeURIComponent(genre);
const tNav = await getTranslations('Navigation');
// Fetch all genres to find the matching one by localized name
const allGenres = await prisma.genre.findMany();
const currentGenre = allGenres.find(g => getLocalizedValue(g.name, locale) === decodedGenre);
if (!currentGenre || !currentGenre.active) {
notFound();
}
const dailyPuzzle = await getOrCreateDailyPuzzle(currentGenre);
// getOrCreateDailyPuzzle likely expects string or needs update.
// Actually, getOrCreateDailyPuzzle takes `genreName: string | null`.
// If I pass the JSON object, it might fail.
// But wait, the DB schema for DailyPuzzle stores `genreId`.
// `getOrCreateDailyPuzzle` probably looks up genre by name.
// I should check `lib/dailyPuzzle.ts`.
// For now, I'll pass the localized name, but that might be risky if it tries to create a genre (unlikely).
// Let's assume for now I should pass the localized name if that's what it uses to find/create.
// But if `getOrCreateDailyPuzzle` uses `findUnique({ where: { name: genreName } })`, it will fail because name is JSON.
// I need to update `lib/dailyPuzzle.ts` too!
// I'll mark that as a todo. For now, let's proceed with page creation.
const genres = allGenres.filter(g => g.active);
// Sort
genres.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
const specials = await prisma.special.findMany();
specials.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
const now = new Date();
const activeSpecials = specials.filter(s => {
const isStarted = !s.launchDate || s.launchDate <= now;
const isEnded = s.endDate && s.endDate < now;
return isStarted && !isEnded;
});
const upcomingSpecials = specials.filter(s => {
return s.launchDate && s.launchDate > now;
});
return (
<>
<div style={{ textAlign: 'center', padding: '1rem', background: '#f3f4f6' }}>
<div style={{ display: 'flex', justifyContent: 'center', gap: '1rem', flexWrap: 'wrap', alignItems: 'center' }}>
<Link href="/" style={{ color: '#4b5563', textDecoration: 'none' }}>{tNav('global')}</Link>
{/* Genres */}
{genres.map(g => {
const name = getLocalizedValue(g.name, locale);
return (
<Link
key={g.id}
href={`/${name}`}
style={{
fontWeight: name === decodedGenre ? 'bold' : 'normal',
textDecoration: name === decodedGenre ? 'underline' : 'none',
color: name === decodedGenre ? 'black' : '#4b5563'
}}
>
{name}
</Link>
);
})}
{/* Separator if both exist */}
{genres.length > 0 && activeSpecials.length > 0 && (
<span style={{ color: '#d1d5db' }}>|</span>
)}
{/* Specials */}
{activeSpecials.map(s => {
const name = getLocalizedValue(s.name, locale);
return (
<Link
key={s.id}
href={`/special/${name}`}
style={{
color: '#be185d', // Pink-700
textDecoration: 'none',
fontWeight: '500'
}}
>
{name}
</Link>
);
})}
</div>
{/* Upcoming Specials */}
{upcomingSpecials.length > 0 && (
<div style={{ marginTop: '0.5rem', fontSize: '0.875rem', color: '#666' }}>
Coming soon: {upcomingSpecials.map(s => {
const name = getLocalizedValue(s.name, locale);
return (
<span key={s.id} style={{ marginLeft: '0.5rem' }}>
{name} ({s.launchDate ? new Date(s.launchDate).toLocaleDateString(locale, {
day: '2-digit',
month: '2-digit',
year: 'numeric',
timeZone: process.env.TZ
}) : ''})
{s.curator && <span style={{ fontStyle: 'italic', marginLeft: '0.25rem' }}>Curated by {s.curator}</span>}
</span>
);
})}
</div>
)}
</div>
<NewsSection locale={locale} />
<Game dailyPuzzle={dailyPuzzle} genre={decodedGenre} />
</>
);
}

2156
app/[locale]/admin/page.tsx Normal file

File diff suppressed because it is too large Load Diff

74
app/[locale]/layout.tsx Normal file
View File

@@ -0,0 +1,74 @@
import type { Metadata, Viewport } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import Script from "next/script";
import "../globals.css"; // Adjusted path
import { NextIntlClientProvider } from 'next-intl';
import { getMessages } from 'next-intl/server';
import { notFound } from 'next/navigation';
import { config } from "@/lib/config";
import InstallPrompt from "@/components/InstallPrompt";
import AppFooter from "@/components/AppFooter";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: config.appName,
description: config.appDescription,
};
export const viewport: Viewport = {
themeColor: config.colors.themeColor,
width: "device-width",
initialScale: 1,
maximumScale: 1,
};
export default async function LocaleLayout({
children,
params
}: {
children: React.ReactNode;
params: Promise<{ locale: string }>;
}) {
const { locale } = await params;
console.log('[app/[locale]/layout] params locale:', locale);
// Ensure that the incoming `locale` is valid
if (!['en', 'de'].includes(locale)) {
console.log('[app/[locale]/layout] invalid locale, triggering notFound()');
notFound();
}
// Providing all messages to the client
const messages = await getMessages();
return (
<html lang={locale}>
<head>
<Script
defer
data-domain={config.plausibleDomain}
src={config.plausibleScriptSrc}
strategy="beforeInteractive"
/>
</head>
<body className={`${geistSans.variable} ${geistMono.variable}`}>
<NextIntlClientProvider messages={messages}>
{children}
<InstallPrompt />
<AppFooter />
</NextIntlClientProvider>
</body>
</html>
);
}

138
app/[locale]/page.tsx Normal file
View File

@@ -0,0 +1,138 @@
import Game from '@/components/Game';
import NewsSection from '@/components/NewsSection';
import OnboardingTour from '@/components/OnboardingTour';
import LanguageSwitcher from '@/components/LanguageSwitcher';
import { getOrCreateDailyPuzzle } from '@/lib/dailyPuzzle';
import { Link } from '@/lib/navigation';
import { PrismaClient } from '@prisma/client';
import { getTranslations } from 'next-intl/server';
import { getLocalizedValue } from '@/lib/i18n';
export const dynamic = 'force-dynamic';
const prisma = new PrismaClient();
export default async function Home({
params
}: {
params: { locale: string };
}) {
const { locale } = await params;
const t = await getTranslations('Home');
const tNav = await getTranslations('Navigation');
const dailyPuzzle = await getOrCreateDailyPuzzle(null); // Global puzzle
const genres = await prisma.genre.findMany({
where: { active: true },
});
const specials = await prisma.special.findMany();
// Sort in memory
genres.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
specials.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
const now = new Date();
const activeSpecials = specials.filter(s => {
const isStarted = !s.launchDate || s.launchDate <= now;
const isEnded = s.endDate && s.endDate < now;
return isStarted && !isEnded;
});
const upcomingSpecials = specials.filter(s => {
return s.launchDate && s.launchDate > now;
});
return (
<>
<div id="tour-genres" style={{ textAlign: 'center', padding: '1rem', background: '#f3f4f6' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '0.5rem', maxWidth: '1200px', margin: '0 auto', padding: '0 1rem' }}>
<div style={{ flex: 1 }} />
<div style={{ display: 'flex', justifyContent: 'center', gap: '1rem', flexWrap: 'wrap', alignItems: 'center', flex: 2 }}>
<div className="tooltip">
<Link href="/" style={{ fontWeight: 'bold', textDecoration: 'underline' }}>{tNav('global')}</Link>
<span className="tooltip-text">{t('globalTooltip')}</span>
</div>
{/* Genres */}
{genres.map(g => {
const name = getLocalizedValue(g.name, locale);
const subtitle = getLocalizedValue(g.subtitle, locale);
return (
<div key={g.id} className="tooltip">
<Link href={`/${name}`} style={{ color: '#4b5563', textDecoration: 'none' }}>
{name}
</Link>
{subtitle && <span className="tooltip-text">{subtitle}</span>}
</div>
);
})}
{/* Separator if both exist */}
{genres.length > 0 && activeSpecials.length > 0 && (
<span style={{ color: '#d1d5db' }}>|</span>
)}
{/* Active Specials */}
{activeSpecials.map(s => {
const name = getLocalizedValue(s.name, locale);
const subtitle = getLocalizedValue(s.subtitle, locale);
return (
<div key={s.id} style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
<div className="tooltip">
<Link
href={`/special/${name}`}
style={{
color: '#be185d', // Pink-700
textDecoration: 'none',
fontWeight: '500'
}}
>
{name}
</Link>
{subtitle && <span className="tooltip-text">{subtitle}</span>}
</div>
{s.curator && (
<span style={{ fontSize: '0.75rem', color: '#666' }}>
{t('curatedBy')} {s.curator}
</span>
)}
</div>
);
})}
</div>
{/* Upcoming Specials */}
{upcomingSpecials.length > 0 && (
<div style={{ marginTop: '0.5rem', fontSize: '0.875rem', color: '#666' }}>
{t('comingSoon')}: {upcomingSpecials.map(s => {
const name = getLocalizedValue(s.name, locale);
return (
<span key={s.id} style={{ marginLeft: '0.5rem' }}>
{name} ({s.launchDate ? new Date(s.launchDate).toLocaleDateString(locale, {
day: '2-digit',
month: '2-digit',
year: 'numeric',
timeZone: process.env.TZ
}) : ''})
{s.curator && <span style={{ fontStyle: 'italic', marginLeft: '0.25rem' }}>{t('curatedBy')} {s.curator}</span>}
</span>
);
})}
</div>
)}
</div>
<div style={{ flex: 1, display: 'flex', justifyContent: 'flex-end' }}>
<LanguageSwitcher />
</div>
</div>
<div id="tour-news">
<NewsSection locale={locale} />
</div>
<Game dailyPuzzle={dailyPuzzle} genre={null} />
<OnboardingTour />
</>
);
}

View File

@@ -0,0 +1,125 @@
import Game from '@/components/Game';
import NewsSection from '@/components/NewsSection';
import { getOrCreateSpecialPuzzle } from '@/lib/dailyPuzzle';
import { Link } from '@/lib/navigation';
import { PrismaClient } from '@prisma/client';
import { getLocalizedValue } from '@/lib/i18n';
import { getTranslations } from 'next-intl/server';
export const dynamic = 'force-dynamic';
const prisma = new PrismaClient();
interface PageProps {
params: Promise<{ locale: string; name: string }>;
}
export default async function SpecialPage({ params }: PageProps) {
const { locale, name } = await params;
const decodedName = decodeURIComponent(name);
const tNav = await getTranslations('Navigation');
const allSpecials = await prisma.special.findMany();
const currentSpecial = allSpecials.find(s => getLocalizedValue(s.name, locale) === decodedName);
const now = new Date();
const isStarted = currentSpecial && (!currentSpecial.launchDate || currentSpecial.launchDate <= now);
const isEnded = currentSpecial && (currentSpecial.endDate && currentSpecial.endDate < now);
if (!currentSpecial || !isStarted) {
return (
<div style={{ textAlign: 'center', padding: '2rem' }}>
<h1>Special Not Available</h1>
<p>This special has not launched yet or does not exist.</p>
<Link href="/">{tNav('home')}</Link>
</div>
);
}
if (isEnded) {
return (
<div style={{ textAlign: 'center', padding: '2rem' }}>
<h1>Special Ended</h1>
<p>This special event has ended.</p>
<Link href="/">{tNav('home')}</Link>
</div>
);
}
// Need to handle getOrCreateSpecialPuzzle with localized name or ID
// Ideally pass ID or full object, but existing function takes name string.
// I'll need to update lib/dailyPuzzle.ts to handle this.
const dailyPuzzle = await getOrCreateSpecialPuzzle(currentSpecial);
const genres = await prisma.genre.findMany({
where: { active: true },
});
genres.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
const specials = allSpecials; // Already fetched
specials.sort((a, b) => getLocalizedValue(a.name, locale).localeCompare(getLocalizedValue(b.name, locale)));
const activeSpecials = specials.filter(s => {
const sStarted = !s.launchDate || s.launchDate <= now;
const sEnded = s.endDate && s.endDate < now;
return sStarted && !sEnded;
});
return (
<>
<div style={{ textAlign: 'center', padding: '1rem', background: '#fce7f3' }}>
<div style={{ display: 'flex', justifyContent: 'center', gap: '1rem', flexWrap: 'wrap', alignItems: 'center' }}>
<Link href="/" style={{ color: '#4b5563', textDecoration: 'none' }}>{tNav('global')}</Link>
{/* Genres */}
{genres.map(g => {
const gName = getLocalizedValue(g.name, locale);
return (
<Link
key={g.id}
href={`/${gName}`}
style={{
color: '#4b5563',
textDecoration: 'none'
}}
>
{gName}
</Link>
);
})}
{/* Separator if both exist */}
{genres.length > 0 && activeSpecials.length > 0 && (
<span style={{ color: '#d1d5db' }}>|</span>
)}
{/* Specials */}
{activeSpecials.map(s => {
const sName = getLocalizedValue(s.name, locale);
return (
<Link
key={s.id}
href={`/special/${sName}`}
style={{
fontWeight: sName === decodedName ? 'bold' : 'normal',
textDecoration: sName === decodedName ? 'underline' : 'none',
color: sName === decodedName ? '#9d174d' : '#be185d'
}}
>
{sName}
</Link>
);
})}
</div>
</div>
<NewsSection locale={locale} />
<Game
dailyPuzzle={dailyPuzzle}
genre={decodedName}
isSpecial={true}
maxAttempts={dailyPuzzle?.maxAttempts}
unlockSteps={dailyPuzzle?.unlockSteps}
/>
</>
);
}