我实现了支持多语言的next-intl
.所以我的路径看起来像www.next.com/de
或www.next.com/en
我用的是NextJS 14
美元.
<NextLink href="/support-us">{t("About us")}</NextLink>
例如,当我想导航到/support-us
时,它必须导航到/en/support-us
,而不是我导航到/support-us
,所以它会抛出错误.
NotFoundError:无法在"Node"上执行"removeChild":要执行的 node 被删除不是这个 node 的子 node .
这是我的代码.
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,
};
});