Files
klz-cables.com/lib/slugs.ts
Marc Mintel 1d472062b1
Some checks failed
Build & Deploy KLZ Cables / deploy (push) Failing after 17m15s
i18n slugs fix
2026-01-21 16:33:48 +01:00

78 lines
2.4 KiB
TypeScript

import { getTranslations } from 'next-intl/server';
/**
* Maps a translated slug to original file slug
* @param translatedSlug - The slug from URL (translated)
* @param _locale - The current locale (unused, kept for API consistency)
* @returns The original file slug, or input slug if no mapping exists
*/
export async function mapSlugToFileSlug(translatedSlug: string, _locale: string): Promise<string> {
const t = await getTranslations('Slugs');
// Check pages
if (t.has(`pages.${translatedSlug}`)) {
return t(`pages.${translatedSlug}`);
}
// Check products
if (t.has(`products.${translatedSlug}`)) {
return t(`products.${translatedSlug}`);
}
// Check categories
if (t.has(`categories.${translatedSlug}`)) {
return t(`categories.${translatedSlug}`);
}
// Return original slug if no mapping found
return translatedSlug;
}
/**
* Maps an original file slug to translated slug for a locale
* @param fileSlug - The original file slug
* @param _locale - The target locale (unused, kept for API consistency)
* @returns The translated slug, or input slug if no mapping exists
*/
export async function mapFileSlugToTranslated(fileSlug: string, _locale: string): Promise<string> {
const t = await getTranslations('Slugs');
// Find the key that maps to this file slug
const sections = ['pages', 'products', 'categories'];
for (const section of sections) {
if (t.has(section)) {
const sectionData = t.raw(section);
if (sectionData && typeof sectionData === 'object') {
for (const [translatedSlug, mappedSlug] of Object.entries(sectionData)) {
if (mappedSlug === fileSlug) {
return translatedSlug;
}
}
}
}
}
// Return original slug if no mapping found
return fileSlug;
}
/**
* Gets all translated slugs for a section
* @param section - The section name (pages, products, categories)
* @param _locale - The current locale (unused, kept for API consistency)
* @returns Object mapping translated slugs to file slugs
*/
export async function getSlugMappings(section: 'pages' | 'products' | 'categories', _locale: string): Promise<Record<string, string>> {
const t = await getTranslations('Slugs');
if (t.has(section)) {
const sectionData = t.raw(section);
if (sectionData && typeof (sectionData as any) === 'object') {
return sectionData as Record<string, string>;
}
}
return {};
}