astro-i18next
astro-i18next copied to clipboard
Working with markdown pages (.md)
If anyone is wondering how to translate pages as .md files, here is a solution (example with a privacy policy page) :
- Create where you want the
.md
files, located in their respective language folders. E.g. :(you can use any folder structure as long as the files have the same name and are located in their language folder.)
- Create an astro page that will be the entry point (e.g.
privacy-policy.astro
) - In this page, make use of the Astro.glob function to retreive the
.md
page content :
---
import i18next, { t, changeLanguage } from "i18next";
import { localizePath } from "astro-i18next";
changeLanguage("fr");
const pages = await Astro.glob("../i18n/**/privacy-policy.md");
let Content;
for (const page of pages) {
if (page.file.includes(`/${i18next.language}/`))
Content = page.Content;
}
---
- Now you can make use of the
<Content />
into your .astro file
<Layout title={t("menu.policy")} pageUrl={localizePath("/mentions-legales")}>
<Content />
</Layout>
I don't get it, you didn't put the translations in the public folder?
I don't get it, you didn't put the translations in the public folder?
Nope. Why ?
What are the json files in the i18n folder? Are they not translations?
Yes they are. My personal preference is to put files used as sources at build time in /src
rather than in /public
.