From 3d895f4d7a506c635caba53f737376df624003b5 Mon Sep 17 00:00:00 2001 From: Unchained Date: Tue, 24 Mar 2026 12:30:05 +0200 Subject: [PATCH] refactor: improve locale modularity - 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 --- src/app/[locale]/layout.tsx | 7 ++++--- src/lib/i18n/metadata.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/lib/i18n/metadata.ts diff --git a/src/app/[locale]/layout.tsx b/src/app/[locale]/layout.tsx index 326e078..0f2ad3b 100644 --- a/src/app/[locale]/layout.tsx +++ b/src/app/[locale]/layout.tsx @@ -1,7 +1,7 @@ import { Metadata } from "next"; import { NextIntlClientProvider } from "next-intl"; import { getMessages, setRequestLocale } from "next-intl/server"; -import { SUPPORTED_LOCALES } from "@/lib/i18n/locales"; +import { SUPPORTED_LOCALES, DEFAULT_LOCALE, isValidLocale } from "@/lib/i18n/locales"; const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || "https://dev.manoonoils.com"; @@ -15,11 +15,12 @@ export async function generateMetadata({ params: Promise<{ locale: string }>; }): Promise { const { locale } = await params; - const localePrefix = locale === "sr" ? "" : `/${locale}`; + const validLocale = isValidLocale(locale) ? locale : DEFAULT_LOCALE; + const localePrefix = validLocale === DEFAULT_LOCALE ? "" : `/${validLocale}`; const languages: Record = {}; for (const loc of SUPPORTED_LOCALES) { - const prefix = loc === "sr" ? "" : `/${loc}`; + const prefix = loc === DEFAULT_LOCALE ? "" : `/${loc}`; languages[loc] = `${baseUrl}${prefix}`; } diff --git a/src/lib/i18n/metadata.ts b/src/lib/i18n/metadata.ts new file mode 100644 index 0000000..ab66f2d --- /dev/null +++ b/src/lib/i18n/metadata.ts @@ -0,0 +1,17 @@ +import { DEFAULT_LOCALE, LOCALE_CONFIG, type Locale } from "./locales"; + +export function getMetadataText(locale: Locale, texts: Partial>): string { + return texts[locale] || texts[DEFAULT_LOCALE] || ""; +} + +export function getSaleorLocale(locale: Locale): string { + return LOCALE_CONFIG[locale].saleorLocale; +} + +export function getLocaleDisplayName(locale: Locale): string { + return LOCALE_CONFIG[locale].label; +} + +export function isDefaultLocale(locale: string): boolean { + return locale === DEFAULT_LOCALE; +}