Issue when I am calling getUserLocale function in i18n.js file to get the locale from cookies #1194
Unanswered
vishal-rana1310
asked this question in
Q&A
Replies: 1 comment
-
If you're calling Note that there's an option to setup with i18n routing and then use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am getting this error
Error: Route / with
dynamic = "error"
couldn't be rendered statically because it usedcookies
.Basically getRequestConfig method doesn't have access to coookies
what should i do?
i18n.js file
import { getRequestConfig } from "next-intl/server"; import { getUserLocale } from "./services/locale"; export default getRequestConfig(async () => { // Provide a static locale, fetch a user setting, // read from
cookies(),
headers()`, etc.const locale = await getUserLocale();
// const locale = "en"
return {
locale,
messages: (await import(
../messages/${locale}.json
)).default,};
});`
locale.js file
"use server"; import { getCookies } from "@/i18n"; import { cookies } from "next/headers"; const COOKIE_NAME = "NEXT_LOCALE"; import { defaultLocale } from "../config"; export async function getUserLocale() { const locale = cookies().get(COOKIE_NAME).value; // determine locale from cookies or other logic return locale; } export async function setUserLocale(locale) { await getCookies(); cookies().set(COOKIE_NAME, locale); }
Beta Was this translation helpful? Give feedback.
All reactions