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:
57
src/lib/i18n/productText.ts
Normal file
57
src/lib/i18n/productText.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { Locale } from "./locales";
|
||||
|
||||
const PRODUCT_TEXT: Record<Locale, {
|
||||
defaultShortDescription: string;
|
||||
defaultBenefits: string[];
|
||||
}> = {
|
||||
sr: {
|
||||
defaultShortDescription: "Premium prirodno ulje za vašu rutinu lepote.",
|
||||
defaultBenefits: ["Prirodno", "Organsko", "Bez okrutnosti"],
|
||||
},
|
||||
en: {
|
||||
defaultShortDescription: "Premium natural oil for your beauty routine.",
|
||||
defaultBenefits: ["Natural", "Organic", "Cruelty-free"],
|
||||
},
|
||||
de: {
|
||||
defaultShortDescription: "Premium natürliches Öl für Ihre Schönheitsroutine.",
|
||||
defaultBenefits: ["Natürlich", "Bio", "Tierversuchsfrei"],
|
||||
},
|
||||
fr: {
|
||||
defaultShortDescription: "Huile naturelle premium pour votre routine beauté.",
|
||||
defaultBenefits: ["Naturel", "Bio", "Sans cruauté"],
|
||||
},
|
||||
};
|
||||
|
||||
export function getProductDefaults(locale: Locale) {
|
||||
return PRODUCT_TEXT[locale] || PRODUCT_TEXT.en;
|
||||
}
|
||||
|
||||
export function getTranslatedBenefits(
|
||||
metadataBenefits: string[] | undefined,
|
||||
locale: Locale
|
||||
): string[] {
|
||||
const defaults = PRODUCT_TEXT[locale] || PRODUCT_TEXT.en;
|
||||
|
||||
if (!metadataBenefits || metadataBenefits.length === 0) {
|
||||
return defaults.defaultBenefits;
|
||||
}
|
||||
|
||||
return metadataBenefits.map((benefit, index) => {
|
||||
const trimmed = benefit.trim();
|
||||
if (!trimmed) {
|
||||
return defaults.defaultBenefits[index] || trimmed;
|
||||
}
|
||||
return trimmed;
|
||||
});
|
||||
}
|
||||
|
||||
export function getTranslatedShortDescription(
|
||||
description: string | undefined,
|
||||
locale: Locale
|
||||
): string {
|
||||
if (description && description.trim()) {
|
||||
return description.split('.')[0] + '.';
|
||||
}
|
||||
const defaults = PRODUCT_TEXT[locale] || PRODUCT_TEXT.en;
|
||||
return defaults.defaultShortDescription;
|
||||
}
|
||||
Reference in New Issue
Block a user