summaryrefslogtreecommitdiff
path: root/src/components/SwitchLanguageLink.astro
blob: c8733bc0d8cbda4c49572726dad5ebde824d8662 (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
26
27
28
---
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 newsCurrentLocaleInPath = new RegExp(`(?<=/news/)(${currentLocale.toLocaleLowerCase()})`);
const otherLocalePath = currentPath
  .replace(currentLocaleInPath, `${otherLocale}/`)
  .replace(newsCurrentLocaleInPath, `${otherLocale.toLocaleLowerCase()}`);

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>