refactor: eliminate hardcoded locale comparisons for antifragility

Created centralized helpers:
- src/lib/i18n/pageMetadata.ts: All page metadata (titles, descriptions, alt text)
- src/lib/i18n/productText.ts: Product-specific translated text (shortDescription, benefits)
- src/lib/i18n/metadata.ts: Helper functions for locale handling

Updated all pages to use centralized metadata:
- Homepage: Uses getPageMetadata for title, description, productionAlt
- Products page: Uses getPageMetadata
- Product detail: Uses getPageMetadata + getTranslatedShortDescription/getTranslatedBenefits
- About page: Uses getPageMetadata

ProductDetail component now uses:
- getTranslatedShortDescription() instead of locale comparison
- getTranslatedBenefits() instead of locale comparison

All user-facing text now goes through translation files or centralized helpers.
Adding a new language now requires only:
1. Add to SUPPORTED_LOCALES in locales.ts
2. Add LOCALE_CONFIG entry
3. Add entries to pageMetadata.ts and productText.ts
4. Add translation keys to message files
This commit is contained in:
Unchained
2026-03-24 12:39:38 +02:00
parent 3d895f4d7a
commit 930a9a7614
8 changed files with 237 additions and 54 deletions

View File

@@ -10,30 +10,32 @@ import ProductReviews from "@/components/product/ProductReviews";
import BeforeAfterGallery from "@/components/home/BeforeAfterGallery";
import ProblemSection from "@/components/home/ProblemSection";
import HowItWorks from "@/components/home/HowItWorks";
import { getPageMetadata } from "@/lib/i18n/pageMetadata";
import { isValidLocale, DEFAULT_LOCALE, getSaleorLocale, type Locale } from "@/lib/i18n/locales";
export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params;
setRequestLocale(locale);
const validLocale = isValidLocale(locale) ? locale : DEFAULT_LOCALE;
const metadata = getPageMetadata(validLocale as Locale);
setRequestLocale(validLocale);
return {
title: locale === "sr"
? "ManoonOils - Premium prirodna ulja za negu kose i kože"
: "ManoonOils - Premium Natural Oils for Hair & Skin",
description: locale === "sr"
? "Otkrijte našu premium kolekciju prirodnih ulja za negu kose i kože."
: "Discover our premium collection of natural oils for hair and skin care.",
title: metadata.home.title,
description: metadata.home.description,
};
}
export default async function Homepage({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params;
setRequestLocale(locale);
const validLocale = isValidLocale(locale) ? locale : DEFAULT_LOCALE;
setRequestLocale(validLocale);
const t = await getTranslations("Home");
const tBenefits = await getTranslations("Benefits");
const metadata = getPageMetadata(validLocale as Locale);
const productLocale = locale === "sr" ? "SR" : "EN";
const saleorLocale = getSaleorLocale(validLocale as Locale);
let products: any[] = [];
try {
products = await getProducts(productLocale);
products = await getProducts(saleorLocale);
} catch (e) {
console.log("Failed to fetch products during build");
}
@@ -41,7 +43,7 @@ export default async function Homepage({ params }: { params: Promise<{ locale: s
const featuredProducts = products?.slice(0, 4) || [];
const hasProducts = featuredProducts.length > 0;
const basePath = `/${locale}`;
const basePath = `/${validLocale}`;
return (
<>
@@ -122,7 +124,7 @@ export default async function Homepage({ params }: { params: Promise<{ locale: s
<div className="relative aspect-[4/3] bg-[#e8f0f5] rounded-lg overflow-hidden">
<img
src="https://images.unsplash.com/photo-1608571423902-eed4a5ad8108?q=80&w=800&auto=format&fit=crop"
alt={locale === "sr" ? "Proizvodnja prirodnih ulja" : "Natural oils production"}
alt={metadata.home.productionAlt}
className="w-full h-full object-cover"
/>
</div>