export default async function getMessages(locale) {
if (!locale || typeof locale !== 'string') {
console.error(' Invalid or missing locale in getMessages:', locale);
throw new Error('Invalid locale');
}
// loading translation files here
}
Here’s my actual app/[locale]/layout.js:
import { notFound } from 'next/navigation';
import getMessages from '@/i18n/request';
import { setRequestLocale } from 'next-intl/server';
import { NextIntlClientProvider } from 'next-intl';
import LayoutWrapper from '@/components/LayoutWrapper';
import { i18n } from '../../i18n.config';
export const dynamic = 'force-dynamic';
export function generateStaticParams() {
return i18n.locales.map(locale => ({ locale }));
}
export default async function LocaleLayout({ children, params }) {
const { locale } = params;
if (!i18n.locales.includes(locale)) {
notFound();
}
setRequestLocale(locale);
let messages;
try {
const data = await getMessages(locale);
messages = data.messages;
} catch (error) {
console.error(' Erro ao carregar mensagens:', error);
notFound();
}
return (
<html lang={locale}>
<body className="bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-white">
<NextIntlClientProvider locale={locale} messages={messages}>
<LayoutWrapper>{children}</LayoutWrapper>
</NextIntlClientProvider>
</body>
</html>
);
}
next.config.js:
module.exports = {
i18n: {
locales: ['en', 'pt'],
defaultLocale: 'en',
},
experimental: {
serverActions: true,
},
};
Project structure:
app/
└── [locale]/
├── layout.js
└── page.js
i18n/
└── request.js
What I'm trying to understand:
* Why is locale showing as undefined inside getMessages()?
* Is my params extraction wrong in layout.js?
* Am I missing any required middleware or routing logic?
* Any help is hugely appreciated — I’ve tried many things and still can't fix this issue. Thanks in advance!
#App Router + next-intl: `locale` is undefined in layout.js when calling `getMessages(locale)`
1 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
if you are using next 15, params must be awaited