routing.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { commonMask } from "@/utils";
  2. import { createNavigation } from "next-intl/navigation";
  3. import { defineRouting } from "next-intl/routing";
  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. const {
  10. Link: BaseLink,
  11. redirect,
  12. usePathname,
  13. useRouter: useBaseRouter,
  14. } = createNavigation({
  15. locales,
  16. });
  17. export const defaultLocale = locales.at(0) as string;
  18. export const routing = defineRouting({
  19. locales: locales,
  20. defaultLocale: defaultLocale,
  21. localeCookie: { name: "language" },
  22. });
  23. const useRouter = () => {
  24. const router = useBaseRouter();
  25. const cfg: any = { ...router };
  26. Object.keys(router).forEach((key: any) => {
  27. cfg[key] = (...args: any) => {
  28. if (key === "push") {
  29. commonMask.show();
  30. }
  31. (router as any)[key].apply(null, args);
  32. };
  33. });
  34. return cfg;
  35. };
  36. const Link = ({ children, ...props }: any) => {
  37. const { onClick } = props;
  38. const newProps = {
  39. ...props,
  40. };
  41. if (typeof window !== "undefined") {
  42. newProps.onClick = onClick
  43. ? onClick
  44. : () => {
  45. commonMask.show();
  46. };
  47. }
  48. return <BaseLink {...newProps}>{children}</BaseLink>;
  49. };
  50. export { Link, redirect, usePathname, useRouter };