astro-i18next icon indicating copy to clipboard operation
astro-i18next copied to clipboard

Working with markdown pages (.md)

Open neo7-studio-web opened this issue 1 year ago • 4 comments

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. : image (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>

neo7-studio-web avatar Jan 11 '24 19:01 neo7-studio-web

I don't get it, you didn't put the translations in the public folder?

koppk001 avatar Mar 11 '24 14:03 koppk001

I don't get it, you didn't put the translations in the public folder?

Nope. Why ?

neo7-studio-web avatar Mar 11 '24 16:03 neo7-studio-web

What are the json files in the i18n folder? Are they not translations?

koppk001 avatar Mar 11 '24 17:03 koppk001

Yes they are. My personal preference is to put files used as sources at build time in /src rather than in /public.

neo7-studio-web avatar Mar 11 '24 17:03 neo7-studio-web