feat(i18n): add localized navigation and news translations

This commit is contained in:
2026-02-12 21:29:15 +01:00
parent 618319dbc2
commit a7895e4dd9
12 changed files with 687 additions and 164 deletions

View File

@@ -0,0 +1,43 @@
-- CreateTable
CREATE TABLE "PostTranslation" (
"id" TEXT NOT NULL,
"postId" TEXT NOT NULL,
"locale" TEXT NOT NULL,
"title" TEXT NOT NULL,
"excerpt" TEXT,
"body" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "PostTranslation_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "NavigationItemTranslation" (
"id" TEXT NOT NULL,
"navigationItemId" TEXT NOT NULL,
"locale" TEXT NOT NULL,
"label" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "NavigationItemTranslation_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "PostTranslation_locale_idx" ON "PostTranslation"("locale");
-- CreateIndex
CREATE UNIQUE INDEX "PostTranslation_postId_locale_key" ON "PostTranslation"("postId", "locale");
-- CreateIndex
CREATE INDEX "NavigationItemTranslation_locale_idx" ON "NavigationItemTranslation"("locale");
-- CreateIndex
CREATE UNIQUE INDEX "NavigationItemTranslation_navigationItemId_locale_key" ON "NavigationItemTranslation"("navigationItemId", "locale");
-- AddForeignKey
ALTER TABLE "PostTranslation" ADD CONSTRAINT "PostTranslation_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "NavigationItemTranslation" ADD CONSTRAINT "NavigationItemTranslation_navigationItemId_fkey" FOREIGN KEY ("navigationItemId") REFERENCES "NavigationItem"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -16,6 +16,22 @@ model Post {
status String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
translations PostTranslation[]
}
model PostTranslation {
id String @id @default(uuid())
postId String
locale String
title String
excerpt String?
body String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
@@unique([postId, locale])
@@index([locale])
}
model User {
@@ -315,6 +331,7 @@ model NavigationItem {
page Page? @relation(fields: [pageId], references: [id], onDelete: SetNull)
parent NavigationItem? @relation("NavigationItemParent", fields: [parentId], references: [id], onDelete: Cascade)
children NavigationItem[] @relation("NavigationItemParent")
translations NavigationItemTranslation[]
@@index([menuId])
@@index([pageId])
@@ -322,6 +339,19 @@ model NavigationItem {
@@unique([menuId, parentId, sortOrder, label])
}
model NavigationItemTranslation {
id String @id @default(uuid())
navigationItemId String
locale String
label String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
navigationItem NavigationItem @relation(fields: [navigationItemId], references: [id], onDelete: Cascade)
@@unique([navigationItemId, locale])
@@index([locale])
}
model Customer {
id String @id @default(uuid())
name String

View File

@@ -49,6 +49,7 @@ export {
listPublishedPageSlugs,
updateNavigationItem,
updatePage,
upsertNavigationItemTranslation,
upsertPageTranslation,
} from "./pages-navigation"
export {
@@ -56,9 +57,13 @@ export {
deletePost,
getPostById,
getPostBySlug,
getPostBySlugForLocale,
listPosts,
listPostsForLocale,
listPostsWithTranslations,
registerPostCrudAuditHook,
updatePost,
upsertPostTranslation,
} from "./posts"
export type { PublicHeaderBanner, PublicHeaderBannerConfig } from "./settings"
export {

View File

@@ -24,6 +24,9 @@ const { mockDb } = vi.hoisted(() => ({
update: vi.fn(),
delete: vi.fn(),
},
navigationItemTranslation: {
upsert: vi.fn(),
},
},
}))
@@ -38,6 +41,7 @@ import {
getPublishedPageBySlugForLocale,
listPublicNavigation,
updatePage,
upsertNavigationItemTranslation,
upsertPageTranslation,
} from "./pages-navigation"
@@ -112,22 +116,33 @@ describe("pages-navigation service", () => {
slug: "home",
status: "published",
},
translations: [{ locale: "de", label: "Startseite" }],
},
],
})
const navigation = await listPublicNavigation("header")
const navigation = await listPublicNavigation("header", "de")
expect(navigation).toEqual([
{
id: "item-1",
label: "Home",
label: "Startseite",
href: "/",
children: [],
},
])
})
it("validates locale when upserting navigation item translation", async () => {
await expect(() =>
upsertNavigationItemTranslation({
navigationItemId: "550e8400-e29b-41d4-a716-446655440001",
locale: "it",
label: "Home",
}),
).rejects.toThrow()
})
it("validates locale when upserting page translation", async () => {
await expect(() =>
upsertPageTranslation({

View File

@@ -6,6 +6,7 @@ import {
updatePageInputSchema,
upsertPageTranslationInputSchema,
} from "@cms/content"
import { z } from "zod"
import { db } from "./client"
@@ -16,6 +17,13 @@ export type PublicNavigationItem = {
children: PublicNavigationItem[]
}
const supportedLocaleSchema = z.enum(["de", "en", "es", "fr"])
const upsertNavigationItemTranslationInputSchema = z.object({
navigationItemId: z.string().uuid(),
locale: supportedLocaleSchema,
label: z.string().min(1).max(180),
})
function resolvePublishedAt(status: string): Date | null {
return status === "published" ? new Date() : null
}
@@ -159,6 +167,9 @@ export async function listNavigationMenus() {
slug: true,
},
},
translations: {
orderBy: [{ locale: "asc" }],
},
},
},
},
@@ -183,7 +194,12 @@ function resolveNavigationHref(item: {
return null
}
export async function listPublicNavigation(location = "header"): Promise<PublicNavigationItem[]> {
export async function listPublicNavigation(
location = "header",
locale?: string,
): Promise<PublicNavigationItem[]> {
const normalizedLocale = locale ? supportedLocaleSchema.safeParse(locale).data : undefined
const menu = await db.navigationMenu.findFirst({
where: {
location,
@@ -203,6 +219,12 @@ export async function listPublicNavigation(location = "header"): Promise<PublicN
status: true,
},
},
translations: normalizedLocale
? {
where: { locale: normalizedLocale },
take: 1,
}
: false,
},
},
},
@@ -232,7 +254,7 @@ export async function listPublicNavigation(location = "header"): Promise<PublicN
itemMap.set(item.id, {
id: item.id,
label: item.label,
label: item.translations?.[0]?.label ?? item.label,
href,
parentId: item.parentId,
children: [],
@@ -298,3 +320,20 @@ export async function deleteNavigationItem(id: string) {
where: { id },
})
}
export async function upsertNavigationItemTranslation(input: unknown) {
const payload = upsertNavigationItemTranslationInputSchema.parse(input)
return db.navigationItemTranslation.upsert({
where: {
navigationItemId_locale: {
navigationItemId: payload.navigationItemId,
locale: payload.locale,
},
},
create: payload,
update: {
label: payload.label,
},
})
}

View File

@@ -9,6 +9,9 @@ const { mockDb } = vi.hoisted(() => ({
update: vi.fn(),
delete: vi.fn(),
},
postTranslation: {
upsert: vi.fn(),
},
},
}))
@@ -16,7 +19,15 @@ vi.mock("./client", () => ({
db: mockDb,
}))
import { createPost, getPostBySlug, listPosts, updatePost } from "./posts"
import {
createPost,
getPostBySlug,
getPostBySlugForLocale,
listPosts,
listPostsForLocale,
updatePost,
upsertPostTranslation,
} from "./posts"
describe("posts service", () => {
beforeEach(() => {
@@ -25,6 +36,7 @@ describe("posts service", () => {
fn.mockReset()
}
}
mockDb.postTranslation.upsert.mockReset()
})
it("lists posts ordered by update date desc", async () => {
@@ -72,4 +84,63 @@ describe("posts service", () => {
},
})
})
it("upserts post translation and reads localized/fallback post views", async () => {
mockDb.postTranslation.upsert.mockResolvedValue({ id: "pt-1" })
mockDb.post.findUnique
.mockResolvedValueOnce({
id: "post-1",
slug: "hello",
title: "Base title",
excerpt: "Base excerpt",
body: "Base body",
translations: [{ locale: "de", title: "Titel", excerpt: "Auszug", body: "Text" }],
})
.mockResolvedValueOnce({
id: "post-1",
slug: "hello",
title: "Base title",
excerpt: "Base excerpt",
body: "Base body",
translations: [],
})
mockDb.post.findMany.mockResolvedValue([
{
id: "post-1",
slug: "hello",
title: "Base title",
excerpt: "Base excerpt",
body: "Base body",
status: "published",
translations: [{ locale: "de", title: "Titel", excerpt: "Auszug", body: "Text" }],
},
])
await upsertPostTranslation({
postId: "550e8400-e29b-41d4-a716-446655440000",
locale: "de",
title: "Titel",
body: "Text",
})
const localized = await getPostBySlugForLocale("hello", "de")
const fallback = await getPostBySlugForLocale("hello", "fr")
const localizedList = await listPostsForLocale("de")
expect(mockDb.postTranslation.upsert).toHaveBeenCalledTimes(1)
expect(localized?.title).toBe("Titel")
expect(fallback?.title).toBe("Base title")
expect(localizedList[0]?.title).toBe("Titel")
})
it("validates locale for post translations", async () => {
await expect(() =>
upsertPostTranslation({
postId: "550e8400-e29b-41d4-a716-446655440000",
locale: "it",
title: "Titolo",
body: "Testo",
}),
).rejects.toThrow()
})
})

View File

@@ -5,6 +5,7 @@ import {
updatePostInputSchema,
} from "@cms/content"
import { type CrudAuditHook, type CrudMutationContext, createCrudService } from "@cms/crud"
import { z } from "zod"
import type { Post } from "../prisma/generated/client/client"
import { db } from "./client"
@@ -35,6 +36,15 @@ const postRepository = {
}),
}
const supportedLocaleSchema = z.enum(["de", "en", "es", "fr"])
const upsertPostTranslationInputSchema = z.object({
postId: z.string().uuid(),
locale: supportedLocaleSchema,
title: z.string().min(3).max(180),
excerpt: z.string().max(320).nullable().optional(),
body: z.string().min(1),
})
const postAuditHooks: Array<CrudAuditHook<Post>> = []
const postCrudService = createCrudService({
@@ -73,6 +83,100 @@ export async function getPostBySlug(slug: string) {
})
}
export async function getPostBySlugForLocale(slug: string, locale: string) {
const normalizedLocale = supportedLocaleSchema.safeParse(locale).data
const post = await db.post.findUnique({
where: { slug },
include: {
translations: normalizedLocale
? {
where: {
locale: normalizedLocale,
},
take: 1,
}
: false,
},
})
if (!post) {
return null
}
const translation = post.translations?.[0]
return {
...post,
title: translation?.title ?? post.title,
excerpt: translation?.excerpt ?? post.excerpt,
body: translation?.body ?? post.body,
}
}
export async function listPostsForLocale(locale: string) {
const normalizedLocale = supportedLocaleSchema.safeParse(locale).data
const posts = await db.post.findMany({
where: {
status: "published",
},
orderBy: {
updatedAt: "desc",
},
include: {
translations: normalizedLocale
? {
where: { locale: normalizedLocale },
take: 1,
}
: false,
},
})
return posts.map((post) => {
const translation = post.translations?.[0]
return {
...post,
title: translation?.title ?? post.title,
excerpt: translation?.excerpt ?? post.excerpt,
body: translation?.body ?? post.body,
}
})
}
export async function listPostsWithTranslations() {
return db.post.findMany({
orderBy: {
updatedAt: "desc",
},
include: {
translations: {
orderBy: [{ locale: "asc" }],
},
},
})
}
export async function upsertPostTranslation(input: unknown) {
const payload = upsertPostTranslationInputSchema.parse(input)
const { postId, locale, ...data } = payload
return db.postTranslation.upsert({
where: {
postId_locale: {
postId,
locale,
},
},
create: {
postId,
locale,
...data,
},
update: data,
})
}
export async function createPost(input: unknown, context?: CrudMutationContext) {
return postCrudService.create(input, context)
}