- Added src/lib/i18n/metadata.ts with helper functions - Updated [locale]/layout.tsx to use DEFAULT_LOCALE constant - routing.ts already uses centralized SUPPORTED_LOCALES Note: For full antifragility, a larger refactor would centralize all hardcoded locale comparisons for metadata text fallbacks. Currently adding a new language requires: 1. SUPPORTED_LOCALES in locales.ts 2. LOCALE_CONFIG entry 3. Translation keys in all message files
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { Metadata } from "next";
|
|
import { NextIntlClientProvider } from "next-intl";
|
|
import { getMessages, setRequestLocale } from "next-intl/server";
|
|
import { SUPPORTED_LOCALES, DEFAULT_LOCALE, isValidLocale } from "@/lib/i18n/locales";
|
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || "https://dev.manoonoils.com";
|
|
|
|
export function generateStaticParams() {
|
|
return SUPPORTED_LOCALES.map((locale) => ({ locale }));
|
|
}
|
|
|
|
export async function generateMetadata({
|
|
params,
|
|
}: {
|
|
params: Promise<{ locale: string }>;
|
|
}): Promise<Metadata> {
|
|
const { locale } = await params;
|
|
const validLocale = isValidLocale(locale) ? locale : DEFAULT_LOCALE;
|
|
const localePrefix = validLocale === DEFAULT_LOCALE ? "" : `/${validLocale}`;
|
|
|
|
const languages: Record<string, string> = {};
|
|
for (const loc of SUPPORTED_LOCALES) {
|
|
const prefix = loc === DEFAULT_LOCALE ? "" : `/${loc}`;
|
|
languages[loc] = `${baseUrl}${prefix}`;
|
|
}
|
|
|
|
return {
|
|
alternates: {
|
|
canonical: `${baseUrl}${localePrefix}`,
|
|
languages,
|
|
},
|
|
};
|
|
}
|
|
|
|
export default async function LocaleLayout({
|
|
children,
|
|
params,
|
|
}: {
|
|
children: React.ReactNode;
|
|
params: Promise<{ locale: string }>;
|
|
}) {
|
|
const { locale } = await params;
|
|
setRequestLocale(locale);
|
|
const messages = await getMessages();
|
|
|
|
return (
|
|
<NextIntlClientProvider messages={messages}>
|
|
{children}
|
|
</NextIntlClientProvider>
|
|
);
|
|
}
|