fix: setRequestLocale in all server pages, update i18n/request.ts API

This commit is contained in:
Romain bogdanovic 2026-03-28 19:46:46 +01:00
parent 547083ed91
commit bf454b58a3
13 changed files with 29 additions and 15 deletions

View File

@ -1,7 +1,7 @@
import { redirect, notFound } from "next/navigation"; import { redirect, notFound } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { CourseForm } from "../CourseForm"; import { CourseForm } from "../CourseForm";
import { ModuleManager } from "./ModuleManager"; import { ModuleManager } from "./ModuleManager";

View File

@ -1,7 +1,7 @@
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { CourseForm } from "../CourseForm"; import { CourseForm } from "../CourseForm";
@ -11,6 +11,7 @@ export default async function NewCoursePage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "admin" }); const t = await getTranslations({ locale, namespace: "admin" });
const session = await auth(); const session = await auth();

View File

@ -1,7 +1,7 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { AdminCourseActions } from "./AdminCourseActions"; import { AdminCourseActions } from "./AdminCourseActions";
@ -20,6 +20,7 @@ export default async function AdminCoursesPage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "admin" }); const t = await getTranslations({ locale, namespace: "admin" });
const session = await auth(); const session = await auth();

View File

@ -1,7 +1,7 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
@ -12,6 +12,7 @@ export default async function AdminDashboardPage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "admin" }); const t = await getTranslations({ locale, namespace: "admin" });
const session = await auth(); const session = await auth();

View File

@ -1,7 +1,7 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
@ -14,6 +14,7 @@ export default async function AdminStudentsPage({
searchParams: Promise<{ q?: string }>; searchParams: Promise<{ q?: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const { q } = await searchParams; const { q } = await searchParams;
const t = await getTranslations({ locale, namespace: "admin" }); const t = await getTranslations({ locale, namespace: "admin" });
const session = await auth(); const session = await auth();

View File

@ -1,5 +1,5 @@
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
export default async function VerifyPage({ export default async function VerifyPage({
@ -8,6 +8,7 @@ export default async function VerifyPage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "auth" }); const t = await getTranslations({ locale, namespace: "auth" });
return ( return (

View File

@ -1,7 +1,7 @@
import { redirect, notFound } from "next/navigation"; import { redirect, notFound } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { LessonContent } from "./LessonContent"; import { LessonContent } from "./LessonContent";

View File

@ -1,4 +1,4 @@
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import Link from "next/link"; import Link from "next/link";
import { db } from "@/lib/db"; import { db } from "@/lib/db";

View File

@ -1,4 +1,4 @@
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { CourseCard } from "@/components/CourseCard"; import { CourseCard } from "@/components/CourseCard";
@ -17,6 +17,7 @@ export default async function CoursesPage({
searchParams: Promise<{ category?: string; level?: string; q?: string }>; searchParams: Promise<{ category?: string; level?: string; q?: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const { category, level, q } = await searchParams; const { category, level, q } = await searchParams;
const t = await getTranslations({ locale, namespace: "home" }); const t = await getTranslations({ locale, namespace: "home" });
const courseT = await getTranslations({ locale, namespace: "course" }); const courseT = await getTranslations({ locale, namespace: "course" });

View File

@ -1,7 +1,7 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { ProgressBar } from "@/components/ProgressBar"; import { ProgressBar } from "@/components/ProgressBar";
@ -19,6 +19,7 @@ export default async function DashboardPage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "dashboard" }); const t = await getTranslations({ locale, namespace: "dashboard" });
const courseT = await getTranslations({ locale, namespace: "course" }); const courseT = await getTranslations({ locale, namespace: "course" });
const session = await auth(); const session = await auth();

View File

@ -1,5 +1,5 @@
import { NextIntlClientProvider } from "next-intl"; import { NextIntlClientProvider } from "next-intl";
import { getMessages } from "next-intl/server"; import { getMessages, setRequestLocale } from "next-intl/server";
import { notFound } from "next/navigation"; import { notFound } from "next/navigation";
import { auth } from "@/auth"; import { auth } from "@/auth";
import { Nav } from "@/components/Nav"; import { Nav } from "@/components/Nav";
@ -24,6 +24,8 @@ export default async function LocaleLayout({
notFound(); notFound();
} }
setRequestLocale(locale);
const messages = await getMessages(); const messages = await getMessages();
const session = await auth(); const session = await auth();
const userRole = (session?.user as any)?.role; const userRole = (session?.user as any)?.role;

View File

@ -1,5 +1,5 @@
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
import { getTranslations } from "next-intl/server"; import { getTranslations, setRequestLocale } from "next-intl/server";
import Link from "next/link"; import Link from "next/link";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { CourseCard } from "@/components/CourseCard"; import { CourseCard } from "@/components/CourseCard";
@ -11,6 +11,7 @@ export default async function HomePage({
params: Promise<{ locale: string }>; params: Promise<{ locale: string }>;
}) { }) {
const { locale } = await params; const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations({ locale, namespace: "home" }); const t = await getTranslations({ locale, namespace: "home" });
const nav = await getTranslations({ locale, namespace: "nav" }); const nav = await getTranslations({ locale, namespace: "nav" });
const courseT = await getTranslations({ locale, namespace: "course" }); const courseT = await getTranslations({ locale, namespace: "course" });

View File

@ -1,5 +1,9 @@
import { getRequestConfig } from "next-intl/server"; import { getRequestConfig } from "next-intl/server";
export default getRequestConfig(async ({ locale }) => ({ export default getRequestConfig(async ({ requestLocale }) => {
messages: (await import(`../../messages/${locale}.json`)).default, const locale = (await requestLocale) ?? "fr";
})); return {
locale,
messages: (await import(`../../messages/${locale}.json`)).default,
};
});