-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocaleSelector.astro
61 lines (55 loc) · 1.51 KB
/
LocaleSelector.astro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
import { getLocale, getLocaleName, locales, t } from '@lib/i18n';
import type { SiteLocale } from '@lib/datocms/types';
import type { PageUrl } from '@lib/seo';
import '@assets/a11y.css';
const activeLocale = getLocale();
interface Props {
pageUrls: PageUrl[];
}
const { pageUrls } = Astro.props;
const getPageHref = (locale: SiteLocale) => {
const pageUrl = pageUrls.find((pageUrl) => pageUrl.locale === locale);
return pageUrl
? new URL(pageUrl.pathname, Astro.site)
: new URL(`/${locale}/`, Astro.site);
};
---
<locale-selector>
<nav aria-labelledby="locale-selector-title">
<span id="locale-selector-title" class="a11y-sr-only">
{t('select_language')}
</span>
<!-- [html-validate-disable-next no-redundant-role -- see ./README.md] -->
<ul role="list" style="list-style: none;">
{
locales.map((locale) => (
<li>
<a
hreflang={locale}
href={getPageHref(locale as SiteLocale)}
lang={locale}
aria-current={locale === activeLocale ? 'page' : 'false'}
aria-label={getLocaleName(locale)}
>
<abbr title={getLocaleName(locale)}>{locale}</abbr>
</a>
</li>
))
}
</ul>
</nav>
</locale-selector>
<script src="./LocaleSelector.client.ts"></script>
<style>
/* basic styling, can be removed */
ul {
margin: 0;
margin-inline-end: 20px;
padding: 0;
}
li {
display: inline-block;
margin-right: 10px;
}
</style>