i18n slugs fix
Some checks failed
Build & Deploy KLZ Cables / deploy (push) Failing after 17m15s

This commit is contained in:
2026-01-21 16:33:48 +01:00
parent 656852e983
commit 1d472062b1
6 changed files with 42 additions and 35 deletions

View File

@@ -30,8 +30,8 @@ export async function generateMetadata({ params }: ProductPageProps): Promise<Me
const categories = ['low-voltage-cables', 'medium-voltage-cables', 'high-voltage-cables', 'solar-cables'];
if (categories.includes(productSlug)) {
const categoryKey = productSlug.replace(/-cables$/, '').replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const categoryTitle = t(`categories.${categoryKey}.title`);
const categoryDesc = t(`categories.${categoryKey}.description`);
const categoryTitle = t.has(`categories.${categoryKey}.title`) ? t(`categories.${categoryKey}.title`) : productSlug;
const categoryDesc = t.has(`categories.${categoryKey}.description`) ? t(`categories.${categoryKey}.description`) : '';
return {
title: categoryTitle,
@@ -60,6 +60,10 @@ export async function generateMetadata({ params }: ProductPageProps): Promise<Me
const product = await getProductBySlug(productSlug, locale);
if (!product) return {};
const categorySlug = slug[0];
const categoryKey = categorySlug.replace(/-cables$/, '').replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const categoryTitle = t.has(`categories.${categoryKey}.title`) ? t(`categories.${categoryKey}.title`) : categorySlug;
return {
title: product.frontmatter.title,
description: product.frontmatter.description,
@@ -131,7 +135,7 @@ export default async function ProductPage({ params }: ProductPageProps) {
if (categories.includes(productSlug)) {
const allProducts = await getAllProducts(locale);
const categoryKey = productSlug.replace(/-cables$/, '').replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const categoryTitle = t(`categories.${categoryKey}.title`);
const categoryTitle = t.has(`categories.${categoryKey}.title`) ? t(`categories.${categoryKey}.title`) : productSlug;
// Filter products for this category
const filteredProducts = allProducts.filter(p =>
@@ -232,7 +236,7 @@ export default async function ProductPage({ params }: ProductPageProps) {
const isFallback = (product.frontmatter as any).isFallback;
const categorySlug = slug[0];
const categoryKey = categorySlug.replace(/-cables$/, '').replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const categoryTitle = t(`categories.${categoryKey}.title`);
const categoryTitle = t.has(`categories.${categoryKey}.title`) ? t(`categories.${categoryKey}.title`) : categorySlug;
const sidebar = (
<ProductSidebar

View File

@@ -1,4 +1,5 @@
import {getRequestConfig} from 'next-intl/server';
import * as Sentry from '@sentry/nextjs';
export default getRequestConfig(async ({requestLocale}) => {
// This typically corresponds to the `[locale]` segment
@@ -11,6 +12,21 @@ export default getRequestConfig(async ({requestLocale}) => {
return {
locale,
messages: (await import(`../messages/${locale}.json`)).default
messages: (await import(`../messages/${locale}.json`)).default,
onError(error) {
if (error.code === 'MISSING_MESSAGE') {
console.error(error.message);
} else {
console.error(error);
}
Sentry.captureException(error);
},
getMessageFallback({namespace, key, error}) {
const path = [namespace, key].filter((part) => part != null).join('.');
if (error.code === 'MISSING_MESSAGE') {
return path;
}
return 'fallback';
}
};
});

View File

@@ -10,33 +10,18 @@ export async function mapSlugToFileSlug(translatedSlug: string, _locale: 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
if (t.has(`pages.${translatedSlug}`)) {
return t(`pages.${translatedSlug}`);
}
// Check products
try {
const productSlug = t.raw(`products.${translatedSlug}`);
if (productSlug && typeof productSlug === 'string') {
return productSlug;
}
} catch {
// Key doesn't exist, continue
if (t.has(`products.${translatedSlug}`)) {
return t(`products.${translatedSlug}`);
}
// Check categories
try {
const categorySlug = t.raw(`categories.${translatedSlug}`);
if (categorySlug && typeof categorySlug === 'string') {
return categorySlug;
}
} catch {
// Key doesn't exist, continue
if (t.has(`categories.${translatedSlug}`)) {
return t(`categories.${translatedSlug}`);
}
// Return original slug if no mapping found
@@ -56,7 +41,7 @@ export async function mapFileSlugToTranslated(fileSlug: string, _locale: string)
const sections = ['pages', 'products', 'categories'];
for (const section of sections) {
try {
if (t.has(section)) {
const sectionData = t.raw(section);
if (sectionData && typeof sectionData === 'object') {
for (const [translatedSlug, mappedSlug] of Object.entries(sectionData)) {
@@ -65,8 +50,6 @@ export async function mapFileSlugToTranslated(fileSlug: string, _locale: string)
}
}
}
} catch {
// Section doesn't exist, continue
}
}
@@ -83,13 +66,11 @@ export async function mapFileSlugToTranslated(fileSlug: string, _locale: string)
export async function getSlugMappings(section: 'pages' | 'products' | 'categories', _locale: string): Promise<Record<string, string>> {
const t = await getTranslations('Slugs');
try {
if (t.has(section)) {
const sectionData = t.raw(section);
if (sectionData && typeof (sectionData as any) === 'object') {
return sectionData as Record<string, string>;
}
} catch {
// Section doesn't exist
}
return {};

View File

@@ -10,7 +10,10 @@
"produkte": "products",
"start": "start",
"danke": "thanks",
"niederspannungskabel": "low-voltage-cables"
"niederspannungskabel": "low-voltage-cables",
"ny2y": "ny2y",
"nycwy": "nycwy",
"nyy": "nyy"
},
"products": {
"n2x2y": "n2x2y",

View File

@@ -10,7 +10,10 @@
"products": "products",
"start": "start",
"thanks": "thanks",
"low-voltage-cables": "low-voltage-cables"
"low-voltage-cables": "low-voltage-cables",
"ny2y": "ny2y",
"nycwy": "nycwy",
"nyy": "nyy"
},
"products": {
"n2x2y": "n2x2y",

File diff suppressed because one or more lines are too long