slug i18n
This commit is contained in:
96
lib/slugs.ts
Normal file
96
lib/slugs.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
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
|
||||
try {
|
||||
const pageSlug = t.raw(`pages.${translatedSlug}`);
|
||||
if (pageSlug && typeof pageSlug === 'string') {
|
||||
return pageSlug;
|
||||
}
|
||||
} catch {
|
||||
// Key doesn't exist, continue
|
||||
}
|
||||
|
||||
// Check products
|
||||
try {
|
||||
const productSlug = t.raw(`products.${translatedSlug}`);
|
||||
if (productSlug && typeof productSlug === 'string') {
|
||||
return productSlug;
|
||||
}
|
||||
} catch {
|
||||
// Key doesn't exist, continue
|
||||
}
|
||||
|
||||
// Check categories
|
||||
try {
|
||||
const categorySlug = t.raw(`categories.${translatedSlug}`);
|
||||
if (categorySlug && typeof categorySlug === 'string') {
|
||||
return categorySlug;
|
||||
}
|
||||
} catch {
|
||||
// Key doesn't exist, continue
|
||||
}
|
||||
|
||||
// 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) {
|
||||
try {
|
||||
const sectionData = t.raw(section);
|
||||
if (sectionData && typeof sectionData === 'object') {
|
||||
for (const [translatedSlug, mappedSlug] of Object.entries(sectionData)) {
|
||||
if (mappedSlug === fileSlug) {
|
||||
return translatedSlug;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Section doesn't exist, continue
|
||||
}
|
||||
}
|
||||
|
||||
// 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');
|
||||
|
||||
try {
|
||||
const sectionData = t.raw(section);
|
||||
if (sectionData && typeof (sectionData as any) === 'object') {
|
||||
return sectionData as Record<string, string>;
|
||||
}
|
||||
} catch {
|
||||
// Section doesn't exist
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
Reference in New Issue
Block a user