Merge pull request #1 from behnamrhp/fix/translation-issues

fix: Fix translation issue for not switching the lang
This commit is contained in:
behnam rahimpour 2025-03-07 11:01:40 +03:00 committed by GitHub
commit 3a44fdafb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 17 deletions

View File

@ -1,11 +1,12 @@
"use client" "use client";
import * as React from "react" import * as React from "react";
import { ThemeProvider as NextThemesProvider } from "next-themes" import { ThemeProvider as NextThemesProvider } from "next-themes";
export function ThemeProvider({ export function ThemeProvider({
children, children,
...props ...props
}: React.ComponentProps<typeof NextThemesProvider>) { }: React.ComponentProps<typeof NextThemesProvider>) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider> // eslint-disable-next-line react/jsx-props-no-spreading
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
} }

View File

@ -20,7 +20,7 @@ export default async function layout(
) { ) {
const { params, children } = props; const { params, children } = props;
const { lang } = await params; const { lang } = await params;
const { resources } = await initI18next({ lng: lang }); await initI18next({ lng: lang });
return ( return (
<html lang={lang} suppressHydrationWarning> <html lang={lang} suppressHydrationWarning>
<body <body
@ -32,9 +32,7 @@ export default async function layout(
enableSystem enableSystem
disableTransitionOnChange disableTransitionOnChange
> >
<TranslationsProvider lng={lang} resources={resources}> <TranslationsProvider lng={lang}>{children}</TranslationsProvider>
{children}
</TranslationsProvider>
</ThemeProvider> </ThemeProvider>
</body> </body>
</html> </html>

View File

@ -2,16 +2,24 @@
import { I18nextProvider } from "react-i18next"; import { I18nextProvider } from "react-i18next";
import { i18nInstance, initI18next, LANGS } from "@/bootstrap/i18n/i18n"; import { i18nInstance, initI18next, LANGS } from "@/bootstrap/i18n/i18n";
import { Resource } from "i18next"; import { PropsWithChildren, useEffect, useState } from "react";
import { PropsWithChildren } from "react"; import storeLang from "@/bootstrap/i18n/store-lang-action";
import { i18n } from "i18next";
export default function TranslationsProvider({ export default function TranslationsProvider(
children, props: PropsWithChildren & { lng: LANGS },
lng, ) {
resources, const { lng, children } = props;
}: PropsWithChildren & { lng: LANGS; resources: Resource }) { const [i18n, setI18n] = useState<i18n>();
if (!resources) return children;
initI18next({ lng, resources }); useEffect(() => {
(async () => {
storeLang(lng);
setI18n((await initI18next({ lng })).i18n);
})();
}, [lng]);
if (!i18n) return null;
return <I18nextProvider i18n={i18nInstance}>{children}</I18nextProvider>; return <I18nextProvider i18n={i18nInstance}>{children}</I18nextProvider>;
} }

View File

@ -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);
}