From 0e011800c956c5626277c10322932e5b9ba60eac Mon Sep 17 00:00:00 2001 From: JOYCEQL <1449239013@qq.com> Date: Wed, 8 Jan 2025 14:15:31 +0800 Subject: [PATCH] perf: language change --- apps/fronted/src/components/shared/LanguageSwitch.tsx | 3 +-- apps/fronted/src/components/shared/UpdateLocale.tsx | 2 +- apps/fronted/src/i18n/config.ts | 2 +- apps/fronted/src/i18n/db.ts | 5 ----- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/apps/fronted/src/components/shared/LanguageSwitch.tsx b/apps/fronted/src/components/shared/LanguageSwitch.tsx index 492ca08..09912fd 100644 --- a/apps/fronted/src/components/shared/LanguageSwitch.tsx +++ b/apps/fronted/src/components/shared/LanguageSwitch.tsx @@ -17,6 +17,7 @@ export default function LanguageSwitch() { const router = useRouter(); const handleLanguageChange = async (newLocale: string) => { + await updateLocale(newLocale); const currentPath = window.location.pathname; if (currentPath === `/${locale}`) { router.push(`/${newLocale}`); @@ -24,8 +25,6 @@ export default function LanguageSwitch() { const newPath = currentPath.replace(`/${locale}/`, `/${newLocale}/`); router.push(newPath); } - await updateLocale(newLocale); - router.refresh(); }; return ( diff --git a/apps/fronted/src/components/shared/UpdateLocale.tsx b/apps/fronted/src/components/shared/UpdateLocale.tsx index c9b3a7e..abb0f5d 100644 --- a/apps/fronted/src/components/shared/UpdateLocale.tsx +++ b/apps/fronted/src/components/shared/UpdateLocale.tsx @@ -3,5 +3,5 @@ import { setUserLocale } from "@/i18n/db"; import { revalidatePath } from "next/cache"; export default async function updateLocale(locale: string) { setUserLocale(locale); - // revalidatePath("/app"); + revalidatePath("/"); } diff --git a/apps/fronted/src/i18n/config.ts b/apps/fronted/src/i18n/config.ts index 46e54c7..fae9885 100644 --- a/apps/fronted/src/i18n/config.ts +++ b/apps/fronted/src/i18n/config.ts @@ -5,5 +5,5 @@ export const defaultLocale: Locale = "zh"; export const localeNames: Record = { zh: "中文", - en: "English" + en: "English", }; diff --git a/apps/fronted/src/i18n/db.ts b/apps/fronted/src/i18n/db.ts index e52d108..1342a62 100644 --- a/apps/fronted/src/i18n/db.ts +++ b/apps/fronted/src/i18n/db.ts @@ -1,11 +1,6 @@ import { cookies } from "next/headers"; import { defaultLocale } from "./config"; -// This cookie name is used by `next-intl` on the public pages too. By -// reading/writing to this locale, we can ensure that the user's locale -// is consistent across public and private pages. In case you save the -// locale of registered users in a database, you can of course also use -// that instead when the user is logged in. const COOKIE_NAME = "NEXT_LOCALE"; export async function getUserLocale() {