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
This commit is contained in:
@@ -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<Metadata> {
|
||||
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<string, string> = {};
|
||||
for (const loc of SUPPORTED_LOCALES) {
|
||||
const prefix = loc === "sr" ? "" : `/${loc}`;
|
||||
const prefix = loc === DEFAULT_LOCALE ? "" : `/${loc}`;
|
||||
languages[loc] = `${baseUrl}${prefix}`;
|
||||
}
|
||||
|
||||
|
||||
17
src/lib/i18n/metadata.ts
Normal file
17
src/lib/i18n/metadata.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { DEFAULT_LOCALE, LOCALE_CONFIG, type Locale } from "./locales";
|
||||
|
||||
export function getMetadataText(locale: Locale, texts: Partial<Record<Locale, string>>): 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;
|
||||
}
|
||||
Reference in New Issue
Block a user