This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
import { routing } from "@/i18n/routing";
|
||||
import { AppLocale, getLocalizedPath, resolveLocale } from "@/lib/locale";
|
||||
|
||||
function getSiteUrl(): URL {
|
||||
return new URL(process.env.NEXT_PUBLIC_SITE_URL ?? "https://mohfarawati.de");
|
||||
}
|
||||
|
||||
function toAbsoluteUrl(pathname: string): string {
|
||||
return new URL(pathname, getSiteUrl()).toString();
|
||||
}
|
||||
|
||||
export function buildLocaleAlternates(pathname: string) {
|
||||
const languages = Object.fromEntries(
|
||||
routing.locales.map((locale) => [locale, toAbsoluteUrl(getLocalizedPath(locale, pathname))]),
|
||||
) as Record<AppLocale, string>;
|
||||
|
||||
return {
|
||||
canonical: toAbsoluteUrl(getLocalizedPath(routing.defaultLocale, pathname)),
|
||||
languages: {
|
||||
...languages,
|
||||
"x-default": toAbsoluteUrl(getLocalizedPath(routing.defaultLocale, pathname)),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
type LocalizedMetadataInput = {
|
||||
locale: string;
|
||||
pathname: string;
|
||||
title: string;
|
||||
description: string;
|
||||
};
|
||||
|
||||
export function buildLocalizedMetadata({
|
||||
locale,
|
||||
pathname,
|
||||
title,
|
||||
description,
|
||||
}: LocalizedMetadataInput): Metadata {
|
||||
const localeKey = resolveLocale(locale);
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
alternates: buildLocaleAlternates(pathname),
|
||||
openGraph: {
|
||||
title,
|
||||
description,
|
||||
url: toAbsoluteUrl(getLocalizedPath(localeKey, pathname)),
|
||||
siteName: "moh-sass",
|
||||
locale: localeKey,
|
||||
type: "website",
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user