From 693f07528e08d5cff2333893ef1d9e883d1aefb1 Mon Sep 17 00:00:00 2001 From: behnam Date: Fri, 7 Mar 2025 10:53:01 +0300 Subject: [PATCH] fix: Fix translation issue for not switching the lang --- .../client/theme-provider/theme-provider.tsx | 9 ++++--- src/app/[lang]/layout.tsx | 6 ++--- src/bootstrap/i18n/i18n-provider.tsx | 26 ++++++++++++------- src/bootstrap/i18n/store-lang-action.ts | 9 +++++++ 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 src/bootstrap/i18n/store-lang-action.ts diff --git a/src/app/[lang]/dashboard/components/client/theme-provider/theme-provider.tsx b/src/app/[lang]/dashboard/components/client/theme-provider/theme-provider.tsx index 6a1ffe4..08ad43b 100644 --- a/src/app/[lang]/dashboard/components/client/theme-provider/theme-provider.tsx +++ b/src/app/[lang]/dashboard/components/client/theme-provider/theme-provider.tsx @@ -1,11 +1,12 @@ -"use client" +"use client"; -import * as React from "react" -import { ThemeProvider as NextThemesProvider } from "next-themes" +import * as React from "react"; +import { ThemeProvider as NextThemesProvider } from "next-themes"; export function ThemeProvider({ children, ...props }: React.ComponentProps) { - return {children} + // eslint-disable-next-line react/jsx-props-no-spreading + return {children}; } diff --git a/src/app/[lang]/layout.tsx b/src/app/[lang]/layout.tsx index 89037c1..7eeae97 100644 --- a/src/app/[lang]/layout.tsx +++ b/src/app/[lang]/layout.tsx @@ -20,7 +20,7 @@ export default async function layout( ) { const { params, children } = props; const { lang } = await params; - const { resources } = await initI18next({ lng: lang }); + await initI18next({ lng: lang }); return ( - - {children} - + {children} diff --git a/src/bootstrap/i18n/i18n-provider.tsx b/src/bootstrap/i18n/i18n-provider.tsx index ed50a29..73d732b 100644 --- a/src/bootstrap/i18n/i18n-provider.tsx +++ b/src/bootstrap/i18n/i18n-provider.tsx @@ -2,16 +2,24 @@ import { I18nextProvider } from "react-i18next"; import { i18nInstance, initI18next, LANGS } from "@/bootstrap/i18n/i18n"; -import { Resource } from "i18next"; -import { PropsWithChildren } from "react"; +import { PropsWithChildren, useEffect, useState } from "react"; +import storeLang from "@/bootstrap/i18n/store-lang-action"; +import { i18n } from "i18next"; -export default function TranslationsProvider({ - children, - lng, - resources, -}: PropsWithChildren & { lng: LANGS; resources: Resource }) { - if (!resources) return children; - initI18next({ lng, resources }); +export default function TranslationsProvider( + props: PropsWithChildren & { lng: LANGS }, +) { + const { lng, children } = props; + const [i18n, setI18n] = useState(); + + useEffect(() => { + (async () => { + storeLang(lng); + setI18n((await initI18next({ lng })).i18n); + })(); + }, [lng]); + + if (!i18n) return null; return {children}; } diff --git a/src/bootstrap/i18n/store-lang-action.ts b/src/bootstrap/i18n/store-lang-action.ts new file mode 100644 index 0000000..2ef60ce --- /dev/null +++ b/src/bootstrap/i18n/store-lang-action.ts @@ -0,0 +1,9 @@ +"use server"; + +import { LANGS } from "@/bootstrap/i18n/i18n"; +import { cookieName } from "@/bootstrap/i18n/settings"; +import { cookies } from "next/headers"; + +export default async function storeLang(lng: LANGS) { + (await cookies()).set(cookieName, lng); +}