Next.js Discord

Discord Forum

Can I retrieve `locale` param from layout.tsx?

Answered
Spot-breasted Oriole posted this in #help-forum
Open in Discord
Spot-breasted OrioleOP
I am trying to set up internationalization but don't want to initialize them for each page, I am trying to build a translationsProvider, used in the Root layout but I would require the locale parameter. Is this possible?
Answered by Spot-breasted Oriole
interface RootLayoutProps {
  children: React.ReactNode;
  params: { locale: string };
}

export default function RootLayout({ children, params }: RootLayoutProps) {
  return (
    <html lang={params.locale}>
      <body className={inter.className}>
        <AuthProvider>
          {children}
          {/* <Footer /> */}
        </AuthProvider>
      </body>
    </html>
  );
}

Works
View full answer

2 Replies

Spot-breasted OrioleOP
const { t } = await initTranslations(locale, ['translations']);
Spot-breasted OrioleOP
interface RootLayoutProps {
  children: React.ReactNode;
  params: { locale: string };
}

export default function RootLayout({ children, params }: RootLayoutProps) {
  return (
    <html lang={params.locale}>
      <body className={inter.className}>
        <AuthProvider>
          {children}
          {/* <Footer /> */}
        </AuthProvider>
      </body>
    </html>
  );
}

Works
Answer