examples(i18n-routing): add localized error pages

- Add localized not-found page with LocaleSwitcher
- Add localized error page with minimal client dictionary
- Add catch-all route to trigger not-found for unknown paths
- Add error translations to dictionaries (en, de, cs)
parent 0a2142cd
import { notFound } from "next/navigation";
export default function CatchAllPage() {
notFound();
}
"use client";
import { useEffect } from "react";
import { useParams } from "next/navigation";
import { getErrorDictionary } from "../../get-error-dictionary";
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
const params = useParams<{ lang: string }>();
const dictionary = getErrorDictionary(params.lang);
useEffect(() => {
console.error(error);
}, [error]);
return (
<div>
<h2>{dictionary.title}</h2>
<button onClick={() => reset()}>{dictionary["try-again"]}</button>
</div>
);
}
import Link from "next/link";
import { headers } from "next/headers";
import { getDictionary } from "../../get-dictionary";
import { i18n, isValidLocale } from "../../i18n-config";
import LocaleSwitcher from "./components/locale-switcher";
export default async function NotFound() {
const headersList = await headers();
// Try to extract locale from referer URL
const referer = headersList.get("referer") || "";
const pathMatch = referer.match(/\/([^\/]+)/);
const urlLocale = pathMatch?.[1];
// Use extracted locale if valid, otherwise fall back to default
const locale =
urlLocale && isValidLocale(urlLocale) ? urlLocale : i18n.defaultLocale;
const dictionary = await getDictionary(locale);
return (
<div>
<LocaleSwitcher />
<h2>{dictionary["not-found"].title}</h2>
<p>{dictionary["not-found"].description}</p>
<Link href={`/${locale}`}>{dictionary["not-found"]["back-home"]}</Link>
</div>
);
}
...@@ -5,5 +5,14 @@ ...@@ -5,5 +5,14 @@
"counter": { "counter": {
"increment": "Přidat", "increment": "Přidat",
"decrement": "Ubrat" "decrement": "Ubrat"
},
"not-found": {
"title": "Stránka nenalezena",
"description": "Požadovaný zdroj nebyl nalezen.",
"back-home": "Zpět domů"
},
"error": {
"title": "Něco se pokazilo!",
"try-again": "Zkusit znovu"
} }
} }
...@@ -5,5 +5,14 @@ ...@@ -5,5 +5,14 @@
"counter": { "counter": {
"increment": "Zuwachs", "increment": "Zuwachs",
"decrement": "Dekrementieren" "decrement": "Dekrementieren"
},
"not-found": {
"title": "Seite nicht gefunden",
"description": "Die angeforderte Ressource konnte nicht gefunden werden.",
"back-home": "Zurück zur Startseite"
},
"error": {
"title": "Etwas ist schief gelaufen!",
"try-again": "Erneut versuchen"
} }
} }
...@@ -5,5 +5,14 @@ ...@@ -5,5 +5,14 @@
"counter": { "counter": {
"increment": "Increment", "increment": "Increment",
"decrement": "Decrement" "decrement": "Decrement"
},
"not-found": {
"title": "Page Not Found",
"description": "Could not find the requested resource.",
"back-home": "Return Home"
},
"error": {
"title": "Something went wrong!",
"try-again": "Try again"
} }
} }
const errorDictionaries = {
en: { title: "Something went wrong!", "try-again": "Try again" },
de: { title: "Etwas ist schief gelaufen!", "try-again": "Erneut versuchen" },
cs: { title: "Něco se pokazilo!", "try-again": "Zkusit znovu" },
};
export type ErrorDictionary = (typeof errorDictionaries)["en"];
export function getErrorDictionary(locale: string): ErrorDictionary {
return (
errorDictionaries[locale as keyof typeof errorDictionaries] ??
errorDictionaries.en
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment