fix(i18n-routing): address PR review comments

- Fix locale extraction in not-found.tsx using getFirstPathSegment()
  helper instead of buggy regex that captured domain from URLs
- Add explanatory comment to get-error-dictionary.ts about the
  trade-off of duplicating error strings for client bundle reliability
- Update isValidLocale() to accept string | undefined for cleaner usage
parent 78fe41e7
import Link from "next/link"; import Link from "next/link";
import { headers } from "next/headers"; import { headers } from "next/headers";
import { getDictionary } from "../../get-dictionary"; import { getDictionary } from "../../get-dictionary";
import { i18n, isValidLocale } from "../../i18n-config"; import { i18n, isValidLocale, getFirstPathSegment } from "../../i18n-config";
import LocaleSwitcher from "./components/locale-switcher";
export default async function NotFound() { export default async function NotFound() {
const headersList = await headers(); const headersList = await headers();
// Try to extract locale from referer URL
const referer = headersList.get("referer") || ""; const referer = headersList.get("referer") || "";
const pathMatch = referer.match(/\/([^\/]+)/); const urlLocale = getFirstPathSegment(referer);
const urlLocale = pathMatch?.[1]; const locale = isValidLocale(urlLocale) ? urlLocale : i18n.defaultLocale;
const dictionary = (await getDictionary(locale))["not-found"];
// Use extracted locale if valid, otherwise fall back to default
const locale =
urlLocale && isValidLocale(urlLocale) ? urlLocale : i18n.defaultLocale;
const dictionary = await getDictionary(locale);
return ( return (
<div> <div>
<LocaleSwitcher /> <h2>{dictionary.title}</h2>
<h2>{dictionary["not-found"].title}</h2> <p>{dictionary.description}</p>
<p>{dictionary["not-found"].description}</p> <Link href={`/${locale}`}>{dictionary["back-home"]}</Link>
<Link href={`/${locale}`}>{dictionary["not-found"]["back-home"]}</Link>
</div> </div>
); );
} }
/**
* Synchronous error dictionary for Client Components.
*
* The full dictionaries are loaded in server components only to avoid shipping
* all translations to the client. These error strings are duplicated here but
* kept minimal so they can be included in the error client bundle for fast
* feedback. This trade-off of duplication for reliability ensures proper
* localised UI even when errors occur.
*/
const errorDictionaries = { const errorDictionaries = {
en: { title: "Something went wrong!", "try-again": "Try again" }, en: { title: "Something went wrong!", "try-again": "Try again" },
de: { title: "Etwas ist schief gelaufen!", "try-again": "Erneut versuchen" }, de: { title: "Etwas ist schief gelaufen!", "try-again": "Erneut versuchen" },
......
...@@ -7,10 +7,18 @@ export const i18n = { ...@@ -7,10 +7,18 @@ export const i18n = {
export type Locale = (typeof i18n)["locales"][number]; export type Locale = (typeof i18n)["locales"][number];
export function isValidLocale(value: string): value is Locale { export function isValidLocale(value: string | undefined): value is Locale {
return i18n.locales.includes(value as Locale); return value !== undefined && i18n.locales.includes(value as Locale);
} }
export function assertValidLocale(value: string): asserts value is Locale { export function assertValidLocale(value: string): asserts value is Locale {
if (!isValidLocale(value)) notFound(); if (!isValidLocale(value)) notFound();
} }
export function getFirstPathSegment(url: string): string | undefined {
try {
return new URL(url).pathname.split("/")[1] || undefined;
} catch {
return undefined;
}
}
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