Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
langs test
bulaienaofutuojsonzhang5e60dbf59d79
diff --git a/src/app/i18n/client.ts b/src/app/i18n/client.tsindex 156a2841..15abd1cc 100644--- a/src/app/i18n/client.ts+++ b/src/app/i18n/client.ts@@ -33,8 +33,13 @@ i18nextlng: undefined, // let detect the language on client sidedetection: {order: ["path", "htmlTag", "cookie", "navigator"],+ caches: ["cookie"], // 只缓存到 cookie,避免其他缓存干扰},preload: runsOnServerSide ? languages : [],+ // 确保语言切换时立即生效+ react: {+ useSuspense: false,+ },});export function useTransClient<@@ -53,14 +58,27 @@ export function useTransClient<i18next.changeLanguage(lng);} else {const [activeLng, setActiveLng] = useState(i18next.resolvedLanguage);++ // 监听 i18next 语言变化useEffect(() => {if (activeLng === i18next.resolvedLanguage) return;setActiveLng(i18next.resolvedLanguage);}, [activeLng, i18next.resolvedLanguage]);++ // 强制同步 URL 参数中的语言到 i18nextuseEffect(() => {- if (!lng || i18next.resolvedLanguage === lng) return;- i18next.changeLanguage(lng);- }, [lng, i18next]);+ if (!lng) return;++ // 如果当前 i18next 语言与 URL 参数不同,强制切换+ if (i18next.resolvedLanguage !== lng) {+ i18next.changeLanguage(lng).then(() => {+ // 语言切换完成后,强制重新渲染+ setActiveLng(lng);+ });+ }+ }, [lng]);++ // 同步 cookieuseEffect(() => {if (i18nextCookie === lng) return;setCookie(cookieName, lng, { path: "/" });diff --git a/src/app/layout/HomeHeader.tsx b/src/app/layout/HomeHeader.tsxindex 1299a23a..22d8fe64 100644--- a/src/app/layout/HomeHeader.tsx+++ b/src/app/layout/HomeHeader.tsx@@ -28,11 +28,16 @@ const HomeHeader = memo(* 切换语言*/const toggleLanguage = () => {- const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";+ const currentLng = userStore.lang;+ const newLng = currentLng === "zh-CN" ? "en" : "zh-CN";userStore.setLang(newLng);- router.push(- `/${newLng}${location.pathname.replace(`/${userStore.lang}`, "")}`,- );++ // 获取当前路径并替换语言前缀+ const currentPath = location.pathname;+ const pathWithoutLang = currentPath.replace(`/${currentLng}`, "") || "/";+ const newPath = `/${newLng}${pathWithoutLang}`;++ router.push(newPath);};/**diff --git a/src/app/layout/LyaoutHeader.tsx b/src/app/layout/LyaoutHeader.tsxindex fc0c45ca..0cbca9dc 100644--- a/src/app/layout/LyaoutHeader.tsx+++ b/src/app/layout/LyaoutHeader.tsx@@ -158,11 +158,16 @@ const LyaoutHeader = memo(const { unreadCount } = useNotification();const toggleLanguage = () => {- const newLng = userStore.lang === "zh-CN" ? "en" : "zh-CN";+ const currentLng = userStore.lang;+ const newLng = currentLng === "zh-CN" ? "en" : "zh-CN";userStore.setLang(newLng);- router.push(- `/${newLng}${location.pathname.replace(`/${userStore.lang}`, "")}`,- );++ // 获取当前路径并替换语言前缀+ const currentPath = location.pathname;+ const pathWithoutLang = currentPath.replace(`/${currentLng}`, "") || "/";+ const newPath = `/${newLng}${pathWithoutLang}`;++ router.push(newPath);};return (diff --git a/src/app/layout/layoutNav.tsx b/src/app/layout/layoutNav.tsxindex a78800c9..352f6233 100644--- a/src/app/layout/layoutNav.tsx+++ b/src/app/layout/layoutNav.tsx@@ -13,6 +13,7 @@ import React, { useEffect, useRef, useState } from "react";import { Drawer, Menu } from "antd";import { useRouter, useSelectedLayoutSegments } from "next/navigation";import { useTransClient } from "@/app/i18n/client";+import { useGetClientLng } from "@/hooks/useSystem";/***@@ -196,7 +197,7 @@ function NavPC() {}, 300);}}>- {getNameTag(v1, 0)}+ {getNameTag(v1, 0)}<ChildNavkey={v1.name}child={v1}@@ -213,6 +214,7 @@ function NavPE() {const [open, setOpen] = useState(false);const router = useRouter();const { t } = useTransClient("route");+ const lng = useGetClientLng();const translatedMenuItems = routerData.map((item) => ({key: item.path || item.name,@@ -223,6 +225,14 @@ function NavPE() {})),}));+ const handleMenuClick = (e: { key: string }) => {+ // 确保使用当前语言前缀+ const targetPath = e.key.startsWith('/') ? e.key : `/${e.key}`;+ const fullPath = `/${lng}${targetPath}`;+ router.push(fullPath);+ setOpen(false);+ };+return (<div className={styles.layoutNavPE}><div onClick={() => setOpen(true)} className={styles["layoutNavPE-menu"]}>@@ -238,10 +248,7 @@ function NavPE() {<MenuclassName="layoutNavPE-menu"selectable={false}- onClick={(e) => {- router.push(e.key);- setOpen(false);- }}+ onClick={handleMenuClick}style={{ width: "100%" }}mode="inline"items={translatedMenuItems}