update project fetching method to select the right language

This commit is contained in:
2026-04-17 09:36:56 +02:00
parent 17d46f8c78
commit 66717a2645
3 changed files with 33 additions and 23 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ type props = {
export default async function Home({ params }: props) { export default async function Home({ params }: props) {
const { locale } = await params; const { locale } = await params;
const projects = getAllProjects().filter((item) => item.ready); const projects = getAllProjects(locale).filter((item) => item.ready);
return ( return (
<main className="bg-brand-beige text-brand-brown flex flex-col"> <main className="bg-brand-beige text-brand-brown flex flex-col">
+15 -7
View File
@@ -14,22 +14,31 @@ type props = {
}>; }>;
}; };
const LOCALES = ["fr", "en"];
export function generateStaticParams() { export function generateStaticParams() {
return getProjectSlugs().flatMap((slug: any) => [ const paths: { locale: string; slug: string }[] = [];
{ locale: "fr", slug },
{ locale: "en", slug }, LOCALES.forEach((locale) => {
]); const slugs = getProjectSlugs(locale);
slugs.forEach((slug) => {
paths.push({ locale, slug });
});
});
return paths;
} }
export default async function ProjectPage({ params }: props) { export default async function ProjectPage({ params }: props) {
const { locale, slug } = await params; const { locale, slug } = await params;
const project = getProjectBySlug(slug);
const project = getProjectBySlug(slug, locale);
if (!project) { if (!project) {
notFound(); notFound();
} }
const otherProjects = getAllProjects() const otherProjects = getAllProjects(locale)
.filter((item: any) => item.slug !== slug && item.ready) .filter((item: any) => item.slug !== slug && item.ready)
.slice(0, 2) .slice(0, 2)
.map((item: any) => ({ .map((item: any) => ({
@@ -48,7 +57,6 @@ export default async function ProjectPage({ params }: props) {
dateLabel={project.dateLabel} dateLabel={project.dateLabel}
cover={project.cover} cover={project.cover}
/> />
<div className="mx-auto max-w-[900px] px-8 md:px-14 lg:px-6"> <div className="mx-auto max-w-[900px] px-8 md:px-14 lg:px-6">
<ProjectMeta <ProjectMeta
dateLabel={project.dateLabel} dateLabel={project.dateLabel}
+14 -12
View File
@@ -3,22 +3,24 @@ import path from "node:path";
import matter from "gray-matter"; import matter from "gray-matter";
import { Project, ProjectFrontmatter } from "./types"; import { Project, ProjectFrontmatter } from "./types";
const projectsDirectory = path.join(process.cwd(), "content/projects"); const CONTENT_DIR = path.join(process.cwd(), "content/projects");
function fileNameToSlug(fileName: string) { export function getProjectSlugs(locale: string) {
return fileName.replace(/\.mdx$/, ""); const localeDirectory = path.join(CONTENT_DIR, locale);
}
if (!fs.existsSync(localeDirectory)) {
return [];
}
export function getProjectSlugs() {
return fs return fs
.readdirSync(projectsDirectory) .readdirSync(localeDirectory)
.filter((file) => file.endsWith(".mdx")) .filter((file) => file.endsWith(".mdx"))
.map(fileNameToSlug); .map((fileName) => fileName.replace(/\.mdx$/, ""));
} }
export function getProjectBySlug(slug: string): Project | null { export function getProjectBySlug(slug: string, locale: string): Project | null {
const realSlug = slug.replace(/\.mdx$/, ""); const realSlug = slug.replace(/\.mdx$/, "");
const fullPath = path.join(projectsDirectory, `${realSlug}.mdx`); const fullPath = path.join(CONTENT_DIR, locale, `${realSlug}.mdx`);
if (!fs.existsSync(fullPath)) { if (!fs.existsSync(fullPath)) {
return null; return null;
@@ -34,9 +36,9 @@ export function getProjectBySlug(slug: string): Project | null {
}; };
} }
export function getAllProjects(): Project[] { export function getAllProjects(locale: string): Project[] {
return getProjectSlugs() return getProjectSlugs(locale)
.map((slug) => getProjectBySlug(slug)) .map((slug) => getProjectBySlug(slug, locale))
.filter((project): project is Project => project !== null) .filter((project): project is Project => project !== null)
.sort((a, b) => a.priority - b.priority); .sort((a, b) => a.priority - b.priority);
} }