Files
kottgard-testing/docs/annotated/src/lib/product-i18n.annotated.ts
T

89 lines
3.4 KiB
TypeScript

/**
* ANNOTATED COPY — every line explained
* Source: src/lib/product-i18n.ts
* NOT used by the app — read this to learn how the real file works
*/
// Import project module (@/ alias = src/ folder in tsconfig)
import { Product } from '@/types';
// (blank line — separates logical blocks for readability)
// TypeScript type alias — union or shorthand for complex types
type Translator = (path: string, params?: Record<string, string | number>) => string;
// (blank line — separates logical blocks for readability)
// Export TypeScript type — defines data shape used across the app
export interface LocalizedProduct {
// Line 6: id: string;
id: string;
// Line 7: slug: string;
slug: string;
// Line 8: category: Product['category'];
category: Product['category'];
// Line 9: name: string;
name: string;
// Line 10: description: string;
description: string;
// Line 11: longDescription: string;
longDescription: string;
// Line 12: price: number;
price: number;
// Line 13: priceUnit: string;
priceUnit: string;
// Line 14: image: string;
image: string;
// Line 15: images: string[];
images: string[];
// Line 16: badge?: string;
badge?: string;
// Line 17: inStock: boolean;
inStock: boolean;
// Line 18: featured: boolean;
featured: boolean;
// Line 19: weight?: string;
weight?: string;
// Line 20: tags: string[];
tags: string[];
// Closing brace — end of block (function, if, object, JSX)
}
// (blank line — separates logical blocks for readability)
// Named export — utility function other files can import
export function localizeProduct(product: Product, t: Translator): LocalizedProduct {
// Line 24: const base = `products.${product.id}`;
const base = `products.${product.id}`;
// (blank line — separates logical blocks for readability)
// Return value from function
return {
// Property or array item — trailing comma allowed in TypeScript
...product,
// Property or array item — trailing comma allowed in TypeScript
name: t(`${base}.name`),
// Property or array item — trailing comma allowed in TypeScript
description: t(`${base}.description`),
// Property or array item — trailing comma allowed in TypeScript
longDescription: t(`${base}.longDescription`),
// Property or array item — trailing comma allowed in TypeScript
priceUnit: t(`priceUnit.${product.priceUnitKey}`),
// Property or array item — trailing comma allowed in TypeScript
badge: product.badgeKey ? t(`badges.${product.badgeKey}`) : undefined,
// Closing brace — end of block (function, if, object, JSX)
};
// Closing brace — end of block (function, if, object, JSX)
}
// (blank line — separates logical blocks for readability)
// Named export — utility function other files can import
export function localizeCategory(
// Property or array item — trailing comma allowed in TypeScript
categoryId: Product['category'],
// Line 38: t: Translator
t: Translator
// Line 39: ): { name: string; description: string } {
): { name: string; description: string } {
// Return value from function
return {
// Property or array item — trailing comma allowed in TypeScript
name: t(`categories.${categoryId}.name`),
// Property or array item — trailing comma allowed in TypeScript
description: t(`categories.${categoryId}.description`),
// Closing brace — end of block (function, if, object, JSX)
};
// Closing brace — end of block (function, if, object, JSX)
}