routing.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { createNavigation } from "next-intl/navigation";
  2. import { defineRouting } from "next-intl/routing";
  3. const modulesFiles = require.context("../../messages", true, /\.json$/);
  4. export const locales = modulesFiles.keys().map((modulePath: string) => {
  5. return modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  6. });
  7. // 路由
  8. const {
  9. Link,
  10. redirect,
  11. usePathname,
  12. useRouter: useBaseRouter,
  13. } = createNavigation({
  14. locales,
  15. });
  16. export const defaultLocale = locales.at(0) as string;
  17. export const routing = defineRouting({
  18. locales: locales,
  19. defaultLocale: defaultLocale,
  20. localeCookie: { name: "language" },
  21. });
  22. const useRouter = () => {
  23. const router = useBaseRouter();
  24. const cfg: any = { ...router };
  25. Object.keys(router).forEach((key: any) => {
  26. cfg[key] = (...args: any) => {
  27. if (key === "push") {
  28. const dom: any = document.querySelector("#globalMask");
  29. if (dom) dom.style.display = "flex";
  30. }
  31. (router as any)[key].apply(null, args);
  32. };
  33. });
  34. return cfg;
  35. };
  36. export { Link, redirect, usePathname, useRouter };