Can't get NEXT_LOCALE cookie/header in request.ts using next-intl
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
I am trying to get my locale cookie in request.ts which is placed in
Code:
/src/i18n/request.ts
next.config.ts
/messages/en.json
/src/i18n/request.ts
but when I try to use (await cookies()).get() an error is thrown: Error: `cookies` was called outside a request scope.
but I can't access cookies without await. In getRequestConfig locale
param was depricated and I get the same error when trying to use requestLocale
. I did everything exactly as https://next-intl.dev/docs/getting-started/app-router/without-i18n-routing instructed. Please help.Code:
/src/i18n/request.ts
import { getRequestConfig } from "next-intl/server";
import { getLocale } from "next-intl/server";
export default getRequestConfig(async ({requestLocale}) => {
//All of these throws an error
// const locale = await getLocale();
// const locale = (await cookies()).get('NEXT_LOCALE)
// const locale = await requestLocale()
const locale = 'en'
return {
locale,
messages: (await import(`../../messages/${locale}.json`)).default,
};
});
next.config.ts
const createNextIntlPlugin = require('next-intl/plugin');
const withNextIntl = createNextIntlPlugin('./src/i18n/request.ts');
/** @type {import('next').NextConfig} */
const nextConfig = {};
module.exports = withNextIntl(nextConfig);
/messages/en.json
{
"title": "english title"
}
5 Replies
Brown bearOP
Also I forgot to add in layout but my layout is done exactly as instructed in next-intl
@Brown bear I am trying to get my locale cookie in request.ts which is placed in `/src/i18n/request.ts` but when I try to use (await cookies()).get() an error is thrown: Error: `cookies` was called outside a request scope. but I can't access cookies without await. In getRequestConfig `locale` param was depricated and I get the same error when trying to use `requestLocale`. I did everything exactly as https://next-intl.dev/docs/getting-started/app-router/without-i18n-routing instructed. Please help.
Code:
/src/i18n/request.ts
import { getRequestConfig } from "next-intl/server";
import { getLocale } from "next-intl/server";
export default getRequestConfig(async ({requestLocale}) => {
//All of these throws an error
// const locale = await getLocale();
// const locale = (await cookies()).get('NEXT_LOCALE)
// const locale = await requestLocale()
const locale = 'en'
return {
locale,
messages: (await import(`../../messages/${locale}.json`)).default,
};
});
next.config.ts
const createNextIntlPlugin = require('next-intl/plugin');
const withNextIntl = createNextIntlPlugin('./src/i18n/request.ts');
/** @type {import('next').NextConfig} */
const nextConfig = {};
module.exports = withNextIntl(nextConfig);
/messages/en.json
{
"title": "english title"
}
West African Crocodile
I'm not sure if it's that simple, but you're not closing the single quotation mark.
// const locale = (await cookies()).get('NEXT_LOCALE)
should be replaced by
const locale = (await cookies()).get('NEXT_LOCALE')
// const locale = (await cookies()).get('NEXT_LOCALE)
should be replaced by
const locale = (await cookies()).get('NEXT_LOCALE')
You can also access the language without needing to request the cookies by simply adding this line at the beginning of the getRequestConfig function:
let locale = await requestLocale;
let locale = await requestLocale;
@West African Crocodile I'm not sure if it's that simple, but you're not closing the single quotation mark.
// const locale = (await cookies()).get('NEXT_LOCALE)
should be replaced by
const locale = (await cookies()).get('NEXT_LOCALE')
Brown bearOP
no this is not fhe problem, i just forgot to add ' in the comment.