-
-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translation not working on client component inside layout #1173
Comments
My team encountered the same issue just yesterday. We even did a test to check this out and we've been able to replicate it. It seems to be a problem with the last version only 'cause we downgraded and it works client side. |
sounds a lot like #1158 to me. |
Yeah, it's the same issue as #1158. I created a repro: https://codesandbox.io/p/devbox/relaxed-kapitsa-59xwtj |
Any updates on this? Because I tend to use one layout tsx for almost all pages where I need consistent layout and this breaks things for me |
+1 running into this issue. |
I might have a solution here, but it might not be suitable for everyone. For me it does only half the work, because I want to use dynamic language changes via queryParams and this solution does not address this issue (but the content is translated and header on reload is translated as well). I hope someone can provide a more suited solution Diving into the next.js docs, I found out that
but there is another option - templates which
That being said, if you have this structure // src/components/translated-component.tsx
'use client';
import useTranslation from 'next-translate/useTranslation';
export const TranslatedComponent = () => {
const { t } = useTranslation('common');
return <div>This is component that needs translation, translation value: {t('test')}</div>;
}; // src/app/layout.tsx
import type {Metadata} from 'next'
import {ReactNode} from "react";
import TranslatedComponent from "@/components/translated-component";
import './globals.css'
export const metadata: Metadata = {
title: 'Website title',
description: 'Website description',
}
export default function LocaleLayout({ children } : { children: ReactNode }) {
return (
<html lang='en' suppressHydrationWarning>
<body>
<TranslatedComponent />
{children}
</body>
</html>
)
} and you run into issue mentioned before, you can take out // src/components/translated-component.tsx
export const TranslatedComponent = ({value} : {value:string}) => {
return <div>This is component that needs translation, translation value: {value}</div>;
}; // src/app/layout.tsx
import type {Metadata} from 'next'
import {ReactNode} from "react";
import './globals.css'
export const metadata: Metadata = {
title: 'Website title',
description: 'Website description',
}
export default function LocaleLayout({ children } : { children: ReactNode }) {
return (
<html lang='en' suppressHydrationWarning>
<body>
// <TranslatedComponent /> // remove this line
{children}
</body>
</html>
)
} // src/app/template.tsx
import TranslatedComponent from "@/components/translated-component";
import useTranslation from 'next-translate/useTranslation';
export default function Template({children}: { children: React.ReactNode }) {
const { t } = useTranslation('common');
return <div>
<TranslatedComponent value{t('test')}/>
{children}
<Footer/>
</div>
} which would result in following nested structure:
Basically template starts doing the work of layout, but with useEffect and other client functions More info in NextJS documentation P.S. I'm not very experienced with next.js and typescript, so if there are any hidden problems with using template file over layout file, I really would like to know. But this setup is working for me so far. |
That works for my use case. I think this is not an error with next-translate but with how nextJS works. I think this issue should be closed with this fix Also, it would be interesting to have this knowledge somewhere in the docs, I think people will be running into this quite frequently I'm not sure where to add it, though 😅 |
@acidfernando This still doesn't solve the issue for people who would like to use the dynamic translation by |
One thing that might be helpful here is the fact that I experience this when I render the When component is not wrapped with client only guard the translation is loaded (but only if the client component is rendered after the |
Do we have some updates regarding this issue? It's pretty huge. I personally had to use another lib to manage translations because of this :-( It's sad because I love this lib 👍 |
+1, this is a huge breaking issue for us... |
This might be a possible solution: // src/app/layout.tsx
import { TestClient } from "@/components/TestClient";
import { TestServer } from "@/components/TestServer";
import I18nProvider from 'next-translate/AppDirI18nProvider';
export default function RootLayout({ children}: {children: React.ReactNode;}) {
const { config, lang, namespaces } = globalThis.__NEXT_TRANSLATE__;
return (
<html lang="en">
<body>
<I18nProvider
lang={lang}
namespaces={namespaces}
config={JSON.parse(JSON.stringify(config))}
>
<h2>Inside LAYOUT</h2>
<TestClient />
<TestServer />
{children}
</I18nProvider>
</body>
</html>
);
} Make sure to import Provider like this
Additionally,
What might also work is to use
@aralroca What do you think of this |
Good to know it @isBatak , probably we can add it internally and solve this |
What version of this package are you using?
next-translate: ^2.6.2
next-translate-plugin: ^2.6.2
next: 14.0.3
What operating system, Node.js, and npm version?
node: 18.17.1
npm: 9.6.7
yarn: 1.22.19
What happened?
I'm using app router on nextjs
In a client component used on a layout, i use
useTranslation
. On server rende, all translations is correct. But on client side the translation is not loaded.The same component work well when used on page.tsx
Screenshot of render from server (with javascript browser disabled)
Screenshot of render from client(with javascript browser enabled)
How reproduce?
Github repository for this issue: https://github.com/zeckaissue/next-translate-issue-client-on-layout
Codesandbox for this issue: https://codesandbox.io/p/github/zeckaissue/next-translate-issue-client-on-layout/main?file=%2Fsrc%2Fapp%2Flayout.tsx%3A21%2C23&workspaceId=08c0217d-10c7-486d-a55b-8417490cf07b
EDIT:
The text was updated successfully, but these errors were encountered: