i18n.ts 716 B

123456789101112131415161718192021
  1. import { createSharedPathnamesNavigation } from "next-intl/navigation";
  2. import { getRequestConfig } from "next-intl/server";
  3. import { notFound } from "next/navigation";
  4. const modulesFiles = require.context("../messages", true, /\.json$/);
  5. export const locales = modulesFiles.keys().map((modulePath: string) => {
  6. return modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  7. });
  8. // 路由
  9. export const { Link, redirect, usePathname, useRouter } = createSharedPathnamesNavigation({
  10. locales,
  11. });
  12. export default getRequestConfig(async ({ locale }) => {
  13. if (!locales.includes(locale)) notFound();
  14. const file = (await import(`../messages/${locale}.json`)).default;
  15. return {
  16. messages: file,
  17. };
  18. });