I implemented a next-intl
for multi-language support. So my paths look like www.next.com/de
or www.next.com/en
I’m using NextJS 14
.
<NextLink href="/support-us">{t("About us")}</NextLink>
And when I want to navigate to for example /support-us
it must navigate me to /en/support-us
instead I get to /support-us
so it throws an error.
NotFoundError: Failed to execute ‘removeChild’ on ‘Node’: The node to
be removed is not a child of this node.
Here is my code.
middleware.ts
import createMiddleware from "next-intl/middleware";
import { locales } from "./i18n";
export default createMiddleware({
// A list of all locales that are supported
locales,
// Used when no locale matches
defaultLocale: "en",
});
export const config = {
// Match only internationalized pathnames
matcher: ["/", "/(en|it|de|nl|fr|sv|es|nb|pt|pl)/:path*"],
};
i18next
import { notFound } from "next/navigation";
import { getRequestConfig } from "next-intl/server";
interface Language {
symbol: string;
name: string;
}
export const supportedLanguages: Language[] = [
{ symbol: "EN", name: "English" },
{ symbol: "DE", name: "Deutsch" },
];
const languageSymbolsLowercase: string[] = supportedLanguages.map((lang) =>
lang.symbol.toLowerCase()
);
const initialLocales: string[] = [];
const combinedLocales: string[] = [
...initialLocales,
...languageSymbolsLowercase,
];
export const locales: string[] = combinedLocales;
export default getRequestConfig(async ({ locale }) => {
if (!locales.includes(locale)) notFound();
return {
messages: (await import(`../messages/${locale}.json`)).default,
};
});
2
Answers
At the end I had to add a
navigation file
as mentioned in the next-intl documentation in there it exports aLink
component which solves the problem and navigates to/[locale]/support-us
for example
/en/support-us
here is mynavigation
fileAnd use the
Link
exported in the navigation fileTo explicitly include the locale in your paths, you can use the
useRouter
hook fromnext/navigation
(innextjs 14
) to access the current locale, and then prepend it to your paths.Here’s how you could adjust your
<NextLink>
usage to incorporate the current locale:UPDATE: I tried to simulate your use case on my end, and I think you can achieve your desired result on the server side by adjusting your
middleware.ts
file. You won’t need to modify your component/page file though.So, below is the updated
middleware.ts
that I tried to revise:I hope this could resolve your issue in a better approach.