32 lines
1.3 KiB
Plaintext
32 lines
1.3 KiB
Plaintext
---
|
|
import { getCollection } from 'astro:content';
|
|
const pathname = Astro.url.pathname.replace(/\/$/, '') || '/';
|
|
const bare = pathname.replace(/^\/(ar|es)(?=\/|$)/, '') || '/';
|
|
const pages = await getCollection('pages', ({ data }) => !data.draft);
|
|
const posts = await getCollection('blog', ({ data }) => !data.draft);
|
|
const available = new Set<string>(['/', '/library', '/es', '/es/library', '/ar', '/ar/library']);
|
|
for (const entry of pages) {
|
|
const prefix = entry.data.lang === 'en' ? '' : `/${entry.data.lang}`;
|
|
const route = entry.data.slug === 'index'
|
|
? prefix || '/'
|
|
: entry.data.slug === 'library'
|
|
? `${prefix}/library`
|
|
: `${prefix}/${entry.data.slug}`;
|
|
available.add(route);
|
|
}
|
|
for (const entry of posts) {
|
|
const prefix = entry.data.lang === 'en' ? '' : `/${entry.data.lang}`;
|
|
available.add(`${prefix}/library/${entry.data.slug}`);
|
|
}
|
|
const choices = [
|
|
{ code: 'en', label: 'EN', href: bare },
|
|
{ code: 'es', label: 'ES', href: `/es${bare === '/' ? '' : bare}` },
|
|
{ code: 'ar', label: 'ع', href: `/ar${bare === '/' ? '' : bare}` }
|
|
].filter(({ href }) => available.has(href));
|
|
---
|
|
<nav class="language-switcher" aria-label="Language">
|
|
{choices.map(({ code, label, href }) => (
|
|
<a href={href} lang={code} aria-current={pathname === href || (href === '/' && pathname === '/') ? 'page' : undefined}>{label}</a>
|
|
))}
|
|
</nav>
|