summaryrefslogtreecommitdiff
path: root/src/components/SwitchLanguageLink.astro
blob: 62de1bad311052b1b586847a308d4f0fcc868f33 (plain)
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
---
import tPage from '$i18n/translations/components/languageLinks';
import tSite from '$i18n/translations/site';
import translate from '$i18n/translate';
import type Locale from '$types/Locale';
import getLocaleFromPath from '$lib/getLocaleFromPath';

// This logic assumes that there are exactly two locales.
const currentLocale = getLocaleFromPath(Astro.url.pathname);
const otherLocale: Locale = currentLocale === 'sco' ? 'en-GB' : 'sco';
const currentPath = Astro.url.pathname;
const currentLocaleInPath = new RegExp(`(?<=^/)(${currentLocale}/)?`);
const otherLocalePath = currentPath.replace(currentLocaleInPath, `${otherLocale}/`);

const t = translate(currentLocale);
---

<a class="link switch-language-link" href={otherLocalePath}>
  {
    t(tPage, {
      key: 'language-link-text',
      language: t(tSite, { key: 'locale', locale: otherLocale }),
    })
  }
</a>