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

@@ -98,14 +98,14 @@ export async function DELETE(request: Request) {
where: { id: puzzle.specialId }
});
if (special) {
newPuzzle = await getOrCreateSpecialPuzzle(special.name);
newPuzzle = await getOrCreateSpecialPuzzle(special);
}
} else if (puzzle.genreId) {
const genre = await prisma.genre.findUnique({
where: { id: puzzle.genreId }
});
if (genre) {
newPuzzle = await getOrCreateDailyPuzzle(genre.name);
newPuzzle = await getOrCreateDailyPuzzle(genre);
}
} else {
newPuzzle = await getOrCreateDailyPuzzle(null);

View File

@@ -2,6 +2,7 @@
import { PrismaClient } from '@prisma/client';
import { requireAdminAuth } from '@/lib/auth';
import { getLocalizedValue } from '@/lib/i18n';
const prisma = new PrismaClient();
@@ -83,7 +84,8 @@ export async function POST(request: Request) {
// Process each song in this batch
for (const song of uncategorizedSongs) {
try {
const genreNames = allGenres.map(g => g.name);
// Use German names for AI categorization (primary language)
const genreNames = allGenres.map(g => getLocalizedValue(g.name, 'de'));
const prompt = `You are a music genre categorization assistant. Given a song title and artist, categorize it into 0-3 of the available genres.
@@ -140,7 +142,7 @@ Your response:`;
// Filter to only valid genres and get their IDs
const genreIds = allGenres
.filter(g => suggestedGenreNames.includes(g.name))
.filter(g => suggestedGenreNames.includes(getLocalizedValue(g.name, 'de')))
.map(g => g.id)
.slice(0, 3); // Max 3 genres
@@ -160,7 +162,7 @@ Your response:`;
title: song.title,
artist: song.artist,
assignedGenres: suggestedGenreNames.filter(name =>
allGenres.some(g => g.name === name)
allGenres.some(g => getLocalizedValue(g.name, 'de') === name)
)
});
}

View File

@@ -1,12 +1,26 @@
import { NextResponse } from 'next/server';
import { getOrCreateDailyPuzzle } from '@/lib/dailyPuzzle';
import { PrismaClient } from '@prisma/client';
import { getLocalizedValue } from '@/lib/i18n';
const prisma = new PrismaClient();
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const genreName = searchParams.get('genre');
const puzzle = await getOrCreateDailyPuzzle(genreName);
let genre = null;
if (genreName) {
// Find genre by localized name (try both locales)
const allGenres = await prisma.genre.findMany();
genre = allGenres.find(g =>
getLocalizedValue(g.name, 'de') === genreName ||
getLocalizedValue(g.name, 'en') === genreName
) || null;
}
const puzzle = await getOrCreateDailyPuzzle(genre);
if (!puzzle) {
return NextResponse.json({ error: 'Failed to get or create puzzle' }, { status: 404 });

View File

@@ -1,19 +1,35 @@
import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
import { requireAdminAuth } from '@/lib/auth';
import { getLocalizedValue } from '@/lib/i18n';
const prisma = new PrismaClient();
export async function GET() {
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const locale = searchParams.get('locale');
const genres = await prisma.genre.findMany({
orderBy: { name: 'asc' },
// orderBy: { name: 'asc' }, // Cannot sort by JSON field easily in SQLite
include: {
_count: {
select: { songs: true }
}
}
});
// Sort in memory if needed, or just return
// If locale is provided, map to localized values
if (locale) {
const localizedGenres = genres.map(g => ({
...g,
name: getLocalizedValue(g.name, locale),
subtitle: getLocalizedValue(g.subtitle, locale)
})).sort((a, b) => a.name.localeCompare(b.name));
return NextResponse.json(localizedGenres);
}
return NextResponse.json(genres);
} catch (error) {
console.error('Error fetching genres:', error);
@@ -29,14 +45,18 @@ export async function POST(request: Request) {
try {
const { name, subtitle, active } = await request.json();
if (!name || typeof name !== 'string') {
if (!name) {
return NextResponse.json({ error: 'Invalid name' }, { status: 400 });
}
// Ensure name is stored as JSON
const nameData = typeof name === 'string' ? { de: name, en: name } : name;
const subtitleData = subtitle ? (typeof subtitle === 'string' ? { de: subtitle, en: subtitle } : subtitle) : null;
const genre = await prisma.genre.create({
data: {
name: name.trim(),
subtitle: subtitle ? subtitle.trim() : null,
name: nameData,
subtitle: subtitleData,
active: active !== undefined ? active : true
},
});
@@ -83,13 +103,14 @@ export async function PUT(request: Request) {
return NextResponse.json({ error: 'Missing id' }, { status: 400 });
}
const updateData: any = {};
if (name) updateData.name = typeof name === 'string' ? { de: name, en: name } : name;
if (subtitle !== undefined) updateData.subtitle = subtitle ? (typeof subtitle === 'string' ? { de: subtitle, en: subtitle } : subtitle) : null;
if (active !== undefined) updateData.active = active;
const genre = await prisma.genre.update({
where: { id: Number(id) },
data: {
...(name && { name: name.trim() }),
subtitle: subtitle ? subtitle.trim() : null,
...(active !== undefined && { active })
},
data: updateData,
});
return NextResponse.json(genre);

View File

@@ -1,6 +1,7 @@
import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
import { requireAdminAuth } from '@/lib/auth';
import { getLocalizedValue } from '@/lib/i18n';
const prisma = new PrismaClient();
@@ -10,6 +11,7 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const limit = parseInt(searchParams.get('limit') || '10');
const featuredOnly = searchParams.get('featured') === 'true';
const locale = searchParams.get('locale');
const where = featuredOnly ? { featured: true } : {};
@@ -27,6 +29,19 @@ export async function GET(request: Request) {
}
});
if (locale) {
const localizedNews = news.map(item => ({
...item,
title: getLocalizedValue(item.title, locale),
content: getLocalizedValue(item.content, locale),
special: item.special ? {
...item.special,
name: getLocalizedValue(item.special.name, locale)
} : null
}));
return NextResponse.json(localizedNews);
}
return NextResponse.json(news);
} catch (error) {
console.error('Error fetching news:', error);
@@ -52,10 +67,14 @@ export async function POST(request: Request) {
);
}
// Ensure title and content are stored as JSON
const titleData = typeof title === 'string' ? { de: title, en: title } : title;
const contentData = typeof content === 'string' ? { de: content, en: content } : content;
const news = await prisma.news.create({
data: {
title,
content,
title: titleData,
content: contentData,
author: author || null,
featured: featured || false,
specialId: specialId || null
@@ -93,8 +112,8 @@ export async function PUT(request: Request) {
}
const updateData: any = {};
if (title !== undefined) updateData.title = title;
if (content !== undefined) updateData.content = content;
if (title !== undefined) updateData.title = typeof title === 'string' ? { de: title, en: title } : title;
if (content !== undefined) updateData.content = typeof content === 'string' ? { de: content, en: content } : content;
if (author !== undefined) updateData.author = author || null;
if (featured !== undefined) updateData.featured = featured;
if (specialId !== undefined) updateData.specialId = specialId || null;

View File

@@ -1,18 +1,32 @@
import { PrismaClient, Special } from '@prisma/client';
import { NextResponse } from 'next/server';
import { requireAdminAuth } from '@/lib/auth';
import { getLocalizedValue } from '@/lib/i18n';
const prisma = new PrismaClient();
export async function GET() {
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const locale = searchParams.get('locale');
const specials = await prisma.special.findMany({
orderBy: { name: 'asc' },
// orderBy: { name: 'asc' },
include: {
_count: {
select: { songs: true }
}
}
});
if (locale) {
const localizedSpecials = specials.map(s => ({
...s,
name: getLocalizedValue(s.name, locale),
subtitle: getLocalizedValue(s.subtitle, locale)
})).sort((a, b) => a.name.localeCompare(b.name));
return NextResponse.json(localizedSpecials);
}
return NextResponse.json(specials);
}
@@ -25,10 +39,15 @@ export async function POST(request: Request) {
if (!name) {
return NextResponse.json({ error: 'Name is required' }, { status: 400 });
}
// Ensure name is stored as JSON
const nameData = typeof name === 'string' ? { de: name, en: name } : name;
const subtitleData = subtitle ? (typeof subtitle === 'string' ? { de: subtitle, en: subtitle } : subtitle) : null;
const special = await prisma.special.create({
data: {
name,
subtitle: subtitle || null,
name: nameData,
subtitle: subtitleData,
maxAttempts: Number(maxAttempts),
unlockSteps,
launchDate: launchDate ? new Date(launchDate) : null,
@@ -61,17 +80,19 @@ export async function PUT(request: Request) {
if (!id) {
return NextResponse.json({ error: 'ID required' }, { status: 400 });
}
const updateData: any = {};
if (name) updateData.name = typeof name === 'string' ? { de: name, en: name } : name;
if (subtitle !== undefined) updateData.subtitle = subtitle ? (typeof subtitle === 'string' ? { de: subtitle, en: subtitle } : subtitle) : null;
if (maxAttempts) updateData.maxAttempts = Number(maxAttempts);
if (unlockSteps) updateData.unlockSteps = unlockSteps;
if (launchDate !== undefined) updateData.launchDate = launchDate ? new Date(launchDate) : null;
if (endDate !== undefined) updateData.endDate = endDate ? new Date(endDate) : null;
if (curator !== undefined) updateData.curator = curator || null;
const updated = await prisma.special.update({
where: { id: Number(id) },
data: {
...(name && { name }),
subtitle: subtitle || null, // Allow clearing or setting
...(maxAttempts && { maxAttempts: Number(maxAttempts) }),
...(unlockSteps && { unlockSteps }),
launchDate: launchDate ? new Date(launchDate) : null,
endDate: endDate ? new Date(endDate) : null,
curator: curator || null,
},
data: updateData,
});
return NextResponse.json(updated);
}