Skip to content

Commit 9768cdf

Browse files
committed
feat: localize content paths
1 parent f33aef9 commit 9768cdf

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

.changeset/tidy-guests-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openscript-ch-website": patch
3+
---
4+
5+
Localize content paths

src/pages/[...locale]/[...pages].astro

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
import Meta from "../../layouts/groups/Meta.astro";
33
import { getCollection, render } from "astro:content";
44
import { i18nPropsAndParams } from "astro-loader-i18n";
5-
import { defaultPropsAndParamsOptions } from "../../utils/paths";
5+
import { defaultPropsAndParamsOptions, replaceSegmentsInPath } from "../../utils/paths";
66
import Shell from "../../layouts/Shell.astro";
77
import Article from "../../layouts/templates/Article.astro";
88
import type { GetStaticPathsOptions } from "astro";
9+
import { parseLocale } from "../../utils/i18n";
910
1011
export const getStaticPaths = async ({ routePattern }: GetStaticPathsOptions) => {
1112
const collection = await getCollection("pages");
1213
1314
return i18nPropsAndParams(collection, {
1415
...defaultPropsAndParamsOptions,
1516
routePattern,
16-
generateSegments: (entry) => ({ pages: `${entry.data.contentPath}/${entry.data.path}` }),
17+
generateSegments: (entry) => {
18+
const contentPath = replaceSegmentsInPath(entry.data.contentPath, parseLocale(entry.data.locale));
19+
return { pages: `${contentPath}/${entry.data.path}` };
20+
},
1721
});
1822
};
1923

src/utils/paths.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createI18nCollection, i18nPropsAndParams, resolvePath as originalResolvePath } from "astro-loader-i18n";
2-
import { C, localeSlugs } from "../site.config";
2+
import { C, localeSlugs, type Locale } from "../site.config";
33
import { getEntry, type CollectionKey, type DataEntryMap } from "astro:content";
44
import type { ImageMetadata } from "astro";
5+
import { parseLocale } from "./i18n";
56

67
const PROTOCOL_DELIMITER = "://";
78

@@ -10,6 +11,13 @@ export const defaultPropsAndParamsOptions = {
1011
segmentTranslations: C.SEGMENT_TRANSLATIONS,
1112
};
1213

14+
export const replaceSegmentsInPath = (path: string, locale: Locale) => {
15+
Object.entries(C.SEGMENT_TRANSLATIONS[locale]).forEach(([key, value]) => {
16+
path = path.replace(key, value);
17+
});
18+
return path;
19+
};
20+
1321
export const resolvePath = (...path: Array<string | number | undefined>) => {
1422
return originalResolvePath(import.meta.env.BASE_URL, ...path);
1523
};
@@ -40,8 +48,9 @@ export const convertReferenceToPath = async (path: string) => {
4048
if (typeof entry.data.path !== "string") throw new Error("Entry title is not a string");
4149

4250
const localeSlug = entry.data.locale === C.DEFAULT_LOCALE ? undefined : entry.data.locale;
51+
const contentPath = replaceSegmentsInPath(entry.data.contentPath, parseLocale(entry.data.locale));
4352

44-
return originalResolvePath(localeSlug, entry.data.contentPath, entry.data.path);
53+
return originalResolvePath(localeSlug, contentPath, entry.data.path);
4554
};
4655

4756
type NavigationItems = DataEntryMap["navigation"]["data"]["data"]["items"];

0 commit comments

Comments
 (0)