This commit is contained in:
+14
-12
@@ -1,21 +1,23 @@
|
||||
import { routing } from "../i18n/routing";
|
||||
import { appLocales } from "../i18n/routing";
|
||||
|
||||
export type AppLocale = (typeof routing.locales)[number];
|
||||
export type AppLocale = (typeof appLocales)[number];
|
||||
|
||||
export function resolveLocale(locale: string): AppLocale {
|
||||
if (locale === "en" || locale === "ar") {
|
||||
return locale;
|
||||
}
|
||||
export const FALLBACK_LOCALE: AppLocale = "de";
|
||||
|
||||
return routing.defaultLocale;
|
||||
export function isSupportedLocale(locale: string | undefined | null): locale is AppLocale {
|
||||
return locale === "de" || locale === "en" || locale === "ar";
|
||||
}
|
||||
|
||||
export function resolveLocale(locale: string | undefined | null, fallbackLocale: AppLocale): AppLocale {
|
||||
return isSupportedLocale(locale) ? locale : fallbackLocale;
|
||||
}
|
||||
|
||||
export function getDirection(locale: string): "ltr" | "rtl" {
|
||||
return resolveLocale(locale) === "ar" ? "rtl" : "ltr";
|
||||
return locale === "ar" ? "rtl" : "ltr";
|
||||
}
|
||||
|
||||
export function stripLocalePrefix(pathname: string): string {
|
||||
for (const locale of routing.locales) {
|
||||
for (const locale of appLocales) {
|
||||
if (pathname === `/${locale}`) {
|
||||
return "/";
|
||||
}
|
||||
@@ -31,7 +33,7 @@ export function stripLocalePrefix(pathname: string): string {
|
||||
export function getLocalizedPath(
|
||||
locale: string,
|
||||
pathname = "/",
|
||||
defaultLocale: AppLocale = routing.defaultLocale,
|
||||
defaultLocale: AppLocale,
|
||||
): string {
|
||||
return getLocalizedPathWithDefault(locale, pathname, defaultLocale);
|
||||
}
|
||||
@@ -39,9 +41,9 @@ export function getLocalizedPath(
|
||||
export function getLocalizedPathWithDefault(
|
||||
locale: string,
|
||||
pathname = "/",
|
||||
defaultLocale: AppLocale = routing.defaultLocale,
|
||||
defaultLocale: AppLocale,
|
||||
): string {
|
||||
const localeKey = resolveLocale(locale);
|
||||
const localeKey = resolveLocale(locale, defaultLocale);
|
||||
const normalizedPath = pathname === "" ? "/" : pathname;
|
||||
const strippedPath = stripLocalePrefix(normalizedPath);
|
||||
|
||||
|
||||
+6
-6
@@ -1,6 +1,6 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
import { routing } from "../i18n/routing";
|
||||
import { appLocales } from "../i18n/routing";
|
||||
import {
|
||||
PAGE_TITLE_TOKEN,
|
||||
SITE_NAME_TOKEN,
|
||||
@@ -22,9 +22,9 @@ function toAbsoluteUrl(pathname: string): string {
|
||||
return new URL(pathname, getSiteUrl()).toString();
|
||||
}
|
||||
|
||||
export function buildLocaleAlternates(pathname: string, defaultLocale = routing.defaultLocale) {
|
||||
export function buildLocaleAlternates(pathname: string, defaultLocale: AppLocale) {
|
||||
const languages = Object.fromEntries(
|
||||
routing.locales.map((locale) => [locale, toAbsoluteUrl(getLocalizedPathWithDefault(locale, pathname, defaultLocale))]),
|
||||
appLocales.map((locale) => [locale, toAbsoluteUrl(getLocalizedPathWithDefault(locale, pathname, defaultLocale))]),
|
||||
) as Record<AppLocale, string>;
|
||||
|
||||
return {
|
||||
@@ -120,8 +120,8 @@ export async function buildLocalizedMetadata({
|
||||
description,
|
||||
applyTitleTemplate,
|
||||
}: LocalizedMetadataInput): Promise<Metadata> {
|
||||
const localeKey = resolveLocale(locale);
|
||||
const [settings, bindings] = await Promise.all([getSiteSettings(), getSiteSettingsMediaBindings()]);
|
||||
const localeKey = resolveLocale(locale, settings.defaultLocale);
|
||||
|
||||
return buildLocalizedMetadataFromConfig({
|
||||
settings,
|
||||
@@ -152,7 +152,7 @@ export function buildLocalizedMetadataFromConfig(input: {
|
||||
description,
|
||||
applyTitleTemplate = true,
|
||||
} = input;
|
||||
const localeKey = resolveLocale(locale);
|
||||
const localeKey = resolveLocale(locale, settings.defaultLocale);
|
||||
const localeSettings = settings.locales[localeKey];
|
||||
const resolvedDescription = description?.trim() || localeSettings.siteDescription;
|
||||
const resolvedTitle = applyTitleTemplate
|
||||
@@ -167,7 +167,7 @@ export function buildLocalizedMetadataFromConfig(input: {
|
||||
openGraph: {
|
||||
title: resolvedTitle,
|
||||
description: resolvedDescription,
|
||||
url: toAbsoluteUrl(getLocalizedPath(localeKey, pathname)),
|
||||
url: toAbsoluteUrl(getLocalizedPath(localeKey, pathname, settings.defaultLocale)),
|
||||
siteName: localeSettings.siteName,
|
||||
locale: localeKey,
|
||||
type: "website",
|
||||
|
||||
+4
-4
@@ -6,7 +6,7 @@ import {
|
||||
type ToastPosition,
|
||||
} from "react-hot-toast";
|
||||
|
||||
import { resolveLocale } from "@/lib/locale";
|
||||
import { FALLBACK_LOCALE, resolveLocale } from "@/lib/locale";
|
||||
|
||||
type ToastVariant = "default" | "success" | "error" | "loading";
|
||||
|
||||
@@ -14,13 +14,13 @@ type ToastMessage = string;
|
||||
|
||||
function getCurrentLocale() {
|
||||
if (typeof window === "undefined") {
|
||||
return "de" as const;
|
||||
return FALLBACK_LOCALE;
|
||||
}
|
||||
|
||||
const pathname = window.location.pathname;
|
||||
const maybeLocale = pathname.split("/")[1] || "de";
|
||||
const maybeLocale = pathname.split("/")[1] || FALLBACK_LOCALE;
|
||||
|
||||
return resolveLocale(maybeLocale);
|
||||
return resolveLocale(maybeLocale, FALLBACK_LOCALE);
|
||||
}
|
||||
|
||||
function getToastPosition(isArabic: boolean): ToastPosition {
|
||||
|
||||
Reference in New Issue
Block a user