Complete public brief locale switching

This commit is contained in:
2569718930@qq.com
2026-06-24 18:53:22 +08:00
parent 3439e0e47a
commit 578f547fa7
10 changed files with 477 additions and 112 deletions
+42 -9
View File
@@ -1,16 +1,37 @@
import type { Metadata } from "next";
import { cookies, headers } from "next/headers";
import { notFound } from "next/navigation";
import { SourceDetailPageView } from "@/components/public-content/PublicContentPages";
import {
LANDING_LOCALE_COOKIE,
LANDING_LOCALE_QUERY_PARAM,
pickLandingLocale,
type LandingLocale,
} from "@/components/landing/landingLocale";
import {
SOURCE_PAGES,
absolutePublicUrl,
getSourcePage,
localizeSourcePage,
sourcePath,
} from "@/content/public-content";
type SourcePageParams = {
slug: string;
};
type SourceSearchParams = Promise<Record<string, string | string[] | undefined>>;
async function resolvePublicContentLocale(searchParams: SourceSearchParams): Promise<LandingLocale> {
const params = await searchParams;
const rawLocale = params[LANDING_LOCALE_QUERY_PARAM];
const queryLocale = Array.isArray(rawLocale) ? rawLocale[0] : rawLocale;
const [cookieStore, headerStore] = await Promise.all([cookies(), headers()]);
return pickLandingLocale(
queryLocale,
cookieStore.get(LANDING_LOCALE_COOKIE)?.value,
headerStore.get("accept-language"),
);
}
export function generateStaticParams() {
return SOURCE_PAGES.map((source) => ({ slug: source.slug }));
@@ -18,10 +39,15 @@ export function generateStaticParams() {
export async function generateMetadata({
params,
searchParams,
}: {
params: Promise<SourcePageParams>;
searchParams: SourceSearchParams;
}): Promise<Metadata> {
const { slug } = await params;
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
const source = getSourcePage(slug);
if (!source) {
@@ -29,17 +55,18 @@ export async function generateMetadata({
title: "Source not found",
};
}
const localizedSource = localizeSourcePage(source, locale);
return {
title: source.title,
description: source.description,
title: localizedSource.title,
description: localizedSource.description,
alternates: {
canonical: sourcePath(source),
},
openGraph: {
type: "article",
title: source.title,
description: source.description,
title: localizedSource.title,
description: localizedSource.description,
url: sourcePath(source),
modifiedTime: source.updatedAt,
},
@@ -48,22 +75,28 @@ export async function generateMetadata({
export default async function SourceDetailPage({
params,
searchParams,
}: {
params: Promise<SourcePageParams>;
searchParams: SourceSearchParams;
}) {
const { slug } = await params;
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
const source = getSourcePage(slug);
if (!source) {
notFound();
}
const localizedSource = localizeSourcePage(source, locale);
const pathname = sourcePath(source);
const jsonLd = {
"@context": "https://schema.org",
"@type": "Dataset",
name: source.title,
description: source.description,
name: localizedSource.title,
description: localizedSource.description,
url: absolutePublicUrl(pathname),
dateModified: source.updatedAt,
creator: {
@@ -83,7 +116,7 @@ export default async function SourceDetailPage({
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
<SourceDetailPageView source={source} />
<SourceDetailPageView source={source} locale={locale} />
</>
);
}