Collection filters
We expose a few utilities to help you handle i18n collections in getStaticPaths
, through collectionFilters
.
import { collectionFilters } from "@astrolicious/i18n/content-collections"
byLocale
Allows you to filter by locale, assuming your entries slugs (or ids) follow the [locale]/[...parts]
pattern:
1import { getCollection } from "astro:content";2import { getLocalePlaceholder } from "i18n:astro";3import { collectionFilters } from "@astrolicious/i18n/content-collections"4
5export const getStaticPaths = (async () => {6 // ...7 const locale = getLocalePlaceholder();8 const posts = await getCollection("posts", (post) =>9 collectionFilters.byLocale(post, { locale }),10 );11 // ...12})
matchingEntries
Allows you to get all entries that match an entry you pass. The usecase is to generate dynamic params:
1import { getCollection } from "astro:content";2import {3 getDefaultLocalePlaceholder,4 getLocalePlaceholder,5} from "i18n:astro";6import {7 collectionFilters,8 generateDynamicParams,9 handleI18nSlug,10} from "@astrolicious/i18n/content-collections";11import type { GetStaticPaths } from "astro";12
13export const getStaticPaths = (async () => {14 const locale = getLocalePlaceholder();15 const defaultLocale = getDefaultLocalePlaceholder();16
17 const posts = await getCollection("posts", (post) =>18 collectionFilters.byLocale(post, { locale }),19 );20
21 return await Promise.all(22 posts.map(async (post) => {23 const equivalentPosts = await getCollection("posts", (p) =>24 collectionFilters.matchingEntries(p, {25 currentEntry: post,26 key: "defaultLocaleVersion",27 locale,28 defaultLocale,29 }),30 );31
32 const dynamicParams = equivalentPosts.map((entry) => {33 const { locale, slug } = handleI18nSlug(entry.slug);34
35 return {36 locale,37 params: {38 slug,39 },40 };41 });42
43 return {44 params: {45 slug: handleI18nSlug(post.slug).slug,46 },47 props: {48 post,49 dynamicParams,50 },51 };52 }),53 );54}) satisfies GetStaticPaths;