Skip to content

Commit

Permalink
Improved support for loading pages with non-existent locales
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Dec 21, 2023
1 parent 9e64e14 commit ce86604
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
13 changes: 12 additions & 1 deletion app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { unstable_setRequestLocale } from "next-intl/server";
import { mkdir, readFile, writeFile } from "fs/promises";
import { Suspense, lazy, type ReactNode, type CSSProperties } from "react";

// Importation des fonctions utilitaires.
import { getLanguages } from "@/utilities/i18n";

// Importation des types.
import type { Metadata, Viewport } from "next";

Expand Down Expand Up @@ -169,9 +172,11 @@ export async function generateMetadata(): Promise<
}

// Génération des paramètres pour les pages statiques.
const languages = getLanguages();

export function generateStaticParams()
{
return [ "en", "fr" ].map( ( locale ) => ( { locale } ) );
return languages.map( ( locale ) => ( { locale } ) );
}

// Création de la police de caractères Inter.
Expand Down Expand Up @@ -203,6 +208,12 @@ export default function Layout( {
// Définition de la langue de la page.
unstable_setRequestLocale( locale );

// Vérification du support de la langue.
if ( !languages.includes( locale ) )
{
return null;
}

// Affichage du rendu HTML de la page.
return (
<html
Expand Down
4 changes: 3 additions & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
import createIntlMiddleware from "next-intl/middleware";
import { type NextRequest, NextResponse } from "next/server";

import { getLanguages } from "./utilities/i18n";
import type { RecaptchaResponse } from "./interfaces/Recaptcha";

export default async function middleware( request: NextRequest )
Expand Down Expand Up @@ -59,7 +61,7 @@ export default async function middleware( request: NextRequest )
// On créé enfin le mécanisme de gestion des langues et traductions.
// Source : https://next-intl-docs.vercel.app/docs/getting-started/app-router-server-components
const handleI18nRouting = createIntlMiddleware( {
locales: [ "en", "fr" ],
locales: getLanguages(),
localePrefix: "never",
defaultLocale: "en"
} );
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"next": "~14.0",
"sharp": "~0.33",
"bcrypt": "~5.1",
"next-intl": "~3.3",
"next-intl": "~3.4",
"deepmerge": "~4.3",
"next-auth": "5.0.0-beta.4",
"nodemailer": "~6.9",
Expand Down
35 changes: 27 additions & 8 deletions utilities/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
//
// Fichier de configuration de la bibliothèque « next-intl ».
// Options de configuration de Next Intl.
// Source : https://next-intl-docs.vercel.app/docs/getting-started/app-router-server-components
//
import deepmerge from "deepmerge";
import { notFound } from "next/navigation";
import { getRequestConfig } from "next-intl/server";
import type { AbstractIntlMessages } from "next-intl";

export default getRequestConfig( async ( { locale } ) => ( {
timeZone: process.env.NEXT_PUBLIC_TIMEZONE,
messages: deepmerge(
( await import( "../public/locales/en.json" ) ).default,
( await import( `../public/locales/${ locale }.json` ) ).default
) as unknown as AbstractIntlMessages
} ) );
export function getLanguages()
{
// Liste des langues disponibles.
return [ "en", "fr" ];
}

export default getRequestConfig( async ( { locale } ) =>
{
// Vérification de la langue demandée par l'utilisateur.
if ( !getLanguages().includes( locale ) )
{
notFound();
}

// Récupération des traductions dans le système de fichiers.
// Note : les traductions manquantes sont fusionnées avec celles de
// la langue par défaut.
return {
timeZone: process.env.NEXT_PUBLIC_TIMEZONE,
messages: deepmerge(
( await import( "../public/locales/en.json" ) ).default,
( await import( `../public/locales/${ locale }.json` ) ).default
) as unknown as AbstractIntlMessages
};
} );
2 changes: 1 addition & 1 deletion utilities/nodemailer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Permet de personnaliser l'apparence et le contenu des courriels envoyés par Next Auth.
// Personnalisation de l'apparence et le contenu des courriels envoyés par Next Auth.
// Source : https://next-auth.js.org/providers/email#customizing-emails
//
import { createTransport } from "nodemailer";
Expand Down

0 comments on commit ce86604

Please sign in to comment.