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 { MethodologyDetailPageView } from "@/components/public-content/PublicContentPages";
import {
LANDING_LOCALE_COOKIE,
LANDING_LOCALE_QUERY_PARAM,
pickLandingLocale,
type LandingLocale,
} from "@/components/landing/landingLocale";
import {
METHODOLOGY_PAGES,
absolutePublicUrl,
getMethodologyPage,
localizeMethodologyPage,
methodologyPath,
} from "@/content/public-content";
type MethodologyPageParams = {
slug: string;
};
type MethodologySearchParams = Promise<Record<string, string | string[] | undefined>>;
async function resolvePublicContentLocale(searchParams: MethodologySearchParams): 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 METHODOLOGY_PAGES.map((page) => ({ slug: page.slug }));
@@ -18,10 +39,15 @@ export function generateStaticParams() {
export async function generateMetadata({
params,
searchParams,
}: {
params: Promise<MethodologyPageParams>;
searchParams: MethodologySearchParams;
}): Promise<Metadata> {
const { slug } = await params;
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
const page = getMethodologyPage(slug);
if (!page) {
@@ -29,17 +55,18 @@ export async function generateMetadata({
title: "Methodology not found",
};
}
const localizedPage = localizeMethodologyPage(page, locale);
return {
title: page.title,
description: page.description,
title: localizedPage.title,
description: localizedPage.description,
alternates: {
canonical: methodologyPath(page),
},
openGraph: {
type: "article",
title: page.title,
description: page.description,
title: localizedPage.title,
description: localizedPage.description,
url: methodologyPath(page),
modifiedTime: page.updatedAt,
},
@@ -48,22 +75,28 @@ export async function generateMetadata({
export default async function MethodologyDetailPage({
params,
searchParams,
}: {
params: Promise<MethodologyPageParams>;
searchParams: MethodologySearchParams;
}) {
const { slug } = await params;
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
const page = getMethodologyPage(slug);
if (!page) {
notFound();
}
const localizedPage = localizeMethodologyPage(page, locale);
const pathname = methodologyPath(page);
const jsonLd = {
"@context": "https://schema.org",
"@type": "TechArticle",
headline: page.title,
description: page.description,
headline: localizedPage.title,
description: localizedPage.description,
dateModified: page.updatedAt,
mainEntityOfPage: absolutePublicUrl(pathname),
author: {
@@ -79,7 +112,7 @@ export default async function MethodologyDetailPage({
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
<MethodologyDetailPageView page={page} />
<MethodologyDetailPageView page={page} locale={locale} />
</>
);
}