|
| 1 | +--- |
| 2 | +import Button from "../components/Button.astro"; |
| 3 | +import Shell from "../layouts/Shell.astro"; |
| 4 | +import Article from "../layouts/templates/Article.astro"; |
| 5 | +--- |
| 6 | + |
| 7 | +<style> |
| 8 | + [data-translation] { |
| 9 | + display: none; |
| 10 | + } |
| 11 | + |
| 12 | + .active[data-translation] { |
| 13 | + display: block; |
| 14 | + } |
| 15 | +</style> |
| 16 | + |
| 17 | +<Shell> |
| 18 | + <div data-translation="en"> |
| 19 | + <Article title="Page not found"> |
| 20 | + <section> |
| 21 | + <p>Sorry, the page you are looking for does not exist.</p> |
| 22 | + <Button href="/en" withArrow>Go to home page</Button> |
| 23 | + </section> |
| 24 | + </Article> |
| 25 | + </div> |
| 26 | + <div data-translation="de"> |
| 27 | + <Article title="Seite nicht gefunden"> |
| 28 | + <section> |
| 29 | + <p>Entschuldigung, die von Ihnen gesuchte Seite existiert nicht.</p> |
| 30 | + <Button href="/" withArrow>Startseite aufrufen</Button> |
| 31 | + </section> |
| 32 | + </Article> |
| 33 | + </div> |
| 34 | + |
| 35 | + <script> |
| 36 | + function register() { |
| 37 | + const localeElements = document.querySelectorAll("[data-translation]"); |
| 38 | + |
| 39 | + for (const element of localeElements) { |
| 40 | + if (!(element instanceof HTMLElement)) continue; |
| 41 | + const { translation } = element.dataset; |
| 42 | + if (!translation) continue; |
| 43 | + |
| 44 | + if (window.location.pathname.startsWith(`/${translation}/`)) { |
| 45 | + element.classList.add("active"); |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (!Array.from(localeElements).some((el) => el instanceof HTMLElement && el.classList.contains("active"))) { |
| 51 | + const defaultElement = document.querySelector(`[data-translation="de"]`); |
| 52 | + if (defaultElement instanceof HTMLElement) { |
| 53 | + defaultElement.classList.add("active"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + register(); |
| 59 | + document.addEventListener("astro:after-swap", register); |
| 60 | + </script> |
| 61 | +</Shell> |
0 commit comments