Files
klz-cables.com/lib/slugs.ts
Marc Mintel d11c27fda0
All checks were successful
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🧪 QA (push) Successful in 56s
Build & Deploy / 🏗️ Build (push) Successful in 2m18s
Build & Deploy / 🚀 Deploy (push) Successful in 15s
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
fix: resolve translation routing bugs for MDX pages/posts, fix QA smoke tests, and automate target host network cleanup
2026-06-10 10:35:06 +02:00

88 lines
2.7 KiB
TypeScript

import enMessages from '../messages/en.json';
import deMessages from '../messages/de.json';
type Messages = typeof enMessages;
const getMessages = (locale: string): Messages => {
return locale === 'de' ? (deMessages as any) : enMessages;
};
/**
* Maps a translated slug to original file slug
* @param translatedSlug - The slug from URL (translated)
* @param locale - The current locale
* @returns The original file slug, or input slug if no mapping exists
*/
export async function mapSlugToFileSlug(translatedSlug: string, locale: string): Promise<string> {
const messages = getMessages(locale);
const slugs = messages.Slugs;
// Check pages
if (slugs.pages && translatedSlug in slugs.pages) {
return slugs.pages[translatedSlug as keyof typeof slugs.pages];
}
// Check products
if (slugs.products && translatedSlug in slugs.products) {
return slugs.products[translatedSlug as keyof typeof slugs.products];
}
// Check categories
if (slugs.categories && translatedSlug in slugs.categories) {
return slugs.categories[translatedSlug as keyof typeof slugs.categories];
}
// Check posts
if ((slugs as any).posts && translatedSlug in (slugs as any).posts) {
return (slugs as any).posts[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
* @returns The translated slug, or input slug if no mapping exists
*/
export async function mapFileSlugToTranslated(fileSlug: string, locale: string): Promise<string> {
const messages = getMessages(locale);
const slugs = messages.Slugs;
const sections = [slugs.pages, slugs.products, slugs.categories, (slugs as any).posts];
for (const sectionData of sections) {
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, posts)
* @param locale - The current locale
* @returns Object mapping translated slugs to file slugs
*/
export async function getSlugMappings(
section: 'pages' | 'products' | 'categories' | 'posts',
locale: string,
): Promise<Record<string, string>> {
const messages = getMessages(locale);
const sectionData = (messages.Slugs as any)[section];
if (sectionData && typeof sectionData === 'object') {
return sectionData as Record<string, string>;
}
return {};
}