feat(i18n): add localized navigation and news translations
This commit is contained in:
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user