diff --git a/ui/i18n.tsx b/ui/i18n.tsx index 661edd1a6..6f92df40b 100644 --- a/ui/i18n.tsx +++ b/ui/i18n.tsx @@ -1,6 +1,7 @@ import { Text } from "@/libs/patternfly/react-core"; import { getRequestConfig } from "next-intl/server"; import { IntlConfig } from "use-intl"; +import { routing } from './i18n/routing'; export const defaultTranslationValues: IntlConfig["defaultTranslationValues"] = { @@ -12,7 +13,17 @@ export const defaultTranslationValues: IntlConfig["defaultTranslationValues"] = text: (text) => {text}, }; -export default getRequestConfig(async ({ locale }) => ({ - messages: (await import(`./messages/${locale}.json`)).default, - defaultTranslationValues, -})); +export default getRequestConfig(async ({ requestLocale }) => { + let locale = await requestLocale; + + // Ensure that the incoming locale is valid + if (!locale || !routing.locales.includes(locale as any)) { + locale = routing.defaultLocale; + } + + return ({ + messages: (await import(`./messages/${locale}.json`)).default, + defaultTranslationValues, + locale, + }); +}); diff --git a/ui/i18n/request.ts b/ui/i18n/request.ts index cb8d9189e..1f3ce786a 100644 --- a/ui/i18n/request.ts +++ b/ui/i18n/request.ts @@ -1,12 +1,16 @@ -import { notFound } from "next/navigation"; import { getRequestConfig } from "next-intl/server"; import { routing } from "@/i18n/routing"; -export default getRequestConfig(async ({ locale }) => { - // Validate that the incoming `locale` parameter is valid - if (!routing.locales.includes(locale as any)) notFound(); +export default getRequestConfig(async ({ requestLocale }) => { + let locale = await requestLocale; - return { - messages: (await import(`../../messages/${locale}.json`)).default, - }; + // Ensure that the incoming locale is valid + if (!locale || !routing.locales.includes(locale as any)) { + locale = routing.defaultLocale; + } + + return { + messages: (await import(`../../messages/${locale}.json`)).default, + locale, + }; }); diff --git a/ui/i18n/routing.ts b/ui/i18n/routing.ts index c605909a4..65637f16c 100644 --- a/ui/i18n/routing.ts +++ b/ui/i18n/routing.ts @@ -1,5 +1,5 @@ import { defineRouting } from "next-intl/routing"; -import { createSharedPathnamesNavigation } from "next-intl/navigation"; +import { createNavigation } from "next-intl/navigation"; export const locales = ["en"] as const; @@ -14,5 +14,13 @@ export const routing = defineRouting({ // Lightweight wrappers around Next.js' navigation APIs // that will consider the routing configuration -export const { Link, redirect, usePathname, useRouter } = - createSharedPathnamesNavigation(routing); +let nav = createNavigation(routing); + +export const { Link, redirect, usePathname, useRouter } = { + Link: nav.Link, + redirect: (path: string) => { + nav.redirect({ href: path, locale: "en" }); + }, + usePathname: nav.usePathname, + useRouter: nav.useRouter, +};