Salem Domani
Salem Domani

Reputation: 23

Astro internationalization

Is there a way to detect the parent folder name to know the current language?

src
└── pages
    ├── fr <-- THIS IS THE PARENT FOLDER
    |   └── index.astro 
    └── index.astro

Things I tried:

I want to access the language from a child component as well

Upvotes: 0

Views: 979

Answers (1)

swithinbank
swithinbank

Reputation: 2069

Probably the simplest way to do this is by looking at Astro.url in your page or component:

---
// The pathname of the current page, e.g. '/fr/'.
const { pathname } = Astro.url;

// The language (parent folder name) will be the second
// item if we split the path at every forward slash.
const lang = pathname.split('/')[1];
---

<p>Language is {lang}.</p>

Upvotes: 1

Related Questions