summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-26 16:58:27 +0100
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-26 16:58:27 +0100
commitf4eb8b6fc931f7ded138dceb2b9df4c63ac7e353 (patch)
treeca189d7ff7beb80a2626c0747a367e745374cf78 /src
parentc8f9f07197fa49ad114a30d4c40bdf24443acc3a (diff)
Differentiates getLocaleFromPath failure strategies
Diffstat (limited to 'src')
-rw-r--r--src/components/Footer.astro4
-rw-r--r--src/components/LallansIssue.astro4
-rw-r--r--src/components/LallansIssueContributions.astro4
-rw-r--r--src/components/LallansIssuesGallery.astro4
-rw-r--r--src/components/Navbar/Logo.astro4
-rw-r--r--src/components/Navbar/NavLinks.astro4
-rw-r--r--src/components/NewsIndex/NewsIndex.astro4
-rw-r--r--src/components/NewsIndex/NewsPreviewLink.astro4
-rw-r--r--src/components/Scotsoun.astro4
-rw-r--r--src/components/ScotsounCdGallery.astro4
-rw-r--r--src/components/SwitchLanguageLink.astro4
-rw-r--r--src/layouts/Layout.astro4
-rw-r--r--src/lib/getLocaleFromPath.ts18
-rw-r--r--src/pages/[locale]/news/index.astro4
-rw-r--r--src/pages/[locale]/rss.xml.ts4
15 files changed, 43 insertions, 31 deletions
diff --git a/src/components/Footer.astro b/src/components/Footer.astro
index 262a487..62b3b82 100644
--- a/src/components/Footer.astro
+++ b/src/components/Footer.astro
@@ -1,10 +1,10 @@
---
import translate from '$i18n/translate';
import tSite from '$i18n/translations/site';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrDefault } from '$lib/getLocaleFromPath';
import Button from './Button.astro';
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrDefault(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/LallansIssue.astro b/src/components/LallansIssue.astro
index 37875e3..efbcdec 100644
--- a/src/components/LallansIssue.astro
+++ b/src/components/LallansIssue.astro
@@ -1,7 +1,7 @@
---
import translate from '$i18n/translate';
import tSite from '$i18n/translations/site';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import relativePath from '$lib/relativePath';
import type LallansIssue from '$types/LallansIssue';
import LallansIssueContributions from './LallansIssueContributions.astro';
@@ -13,7 +13,7 @@ interface Props {
}
const { issue } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/LallansIssueContributions.astro b/src/components/LallansIssueContributions.astro
index 9cf46da..98824de 100644
--- a/src/components/LallansIssueContributions.astro
+++ b/src/components/LallansIssueContributions.astro
@@ -1,7 +1,7 @@
---
import translate from '$i18n/translate';
import tSite from '$i18n/translations/site';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import type { Contribution } from '$types/LallansIssue';
interface Props {
@@ -9,7 +9,7 @@ interface Props {
}
const { contributions } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/LallansIssuesGallery.astro b/src/components/LallansIssuesGallery.astro
index b826525..76209c6 100644
--- a/src/components/LallansIssuesGallery.astro
+++ b/src/components/LallansIssuesGallery.astro
@@ -1,9 +1,9 @@
---
import { getAllLallansIssues } from '$data/lallans';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import type LallansIssue from '$types/LallansIssue';
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const issues = (await getAllLallansIssues()).sort(issueNumberDescending);
function issueNumberDescending(issue1: LallansIssue, issue2: LallansIssue) {
diff --git a/src/components/Navbar/Logo.astro b/src/components/Navbar/Logo.astro
index 6dc372c..dc87d19 100644
--- a/src/components/Navbar/Logo.astro
+++ b/src/components/Navbar/Logo.astro
@@ -1,9 +1,9 @@
---
import translate from '$i18n/translate';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrDefault } from '$lib/getLocaleFromPath';
import tSite from '$i18n/translations/site';
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrDefault(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/Navbar/NavLinks.astro b/src/components/Navbar/NavLinks.astro
index fe2ade0..798c24e 100644
--- a/src/components/Navbar/NavLinks.astro
+++ b/src/components/Navbar/NavLinks.astro
@@ -1,11 +1,11 @@
---
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrDefault } from '$lib/getLocaleFromPath';
import translate from '$i18n/translate';
import tNews from '$i18n/translations/pages/news';
import tFurthsettins from '$i18n/translations/pages/furthsettins';
import tHame from '$i18n/translations/pages/home';
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrDefault(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/NewsIndex/NewsIndex.astro b/src/components/NewsIndex/NewsIndex.astro
index 6738071..ae4c226 100644
--- a/src/components/NewsIndex/NewsIndex.astro
+++ b/src/components/NewsIndex/NewsIndex.astro
@@ -1,7 +1,7 @@
---
import type { CollectionEntry } from 'astro:content';
import NewsPreviewLink from './NewsPreviewLink.astro';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import translate from '$i18n/translate';
import tComponent from '$i18n/translations/components/newsIndex';
interface Props {
@@ -10,7 +10,7 @@ interface Props {
}
const { headingLevel, newsItems } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/NewsIndex/NewsPreviewLink.astro b/src/components/NewsIndex/NewsPreviewLink.astro
index d698a7a..fa01d8d 100644
--- a/src/components/NewsIndex/NewsPreviewLink.astro
+++ b/src/components/NewsIndex/NewsPreviewLink.astro
@@ -1,7 +1,7 @@
---
import translate from '$i18n/translate';
import tComponent from '$i18n/translations/components/newsIndex';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import { getHref } from '$lib/newsUtils';
import type { CollectionEntry } from 'astro:content';
@@ -11,7 +11,7 @@ interface Props {
}
const { headingLevel, newsItem } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/Scotsoun.astro b/src/components/Scotsoun.astro
index c1d8fdf..4825423 100644
--- a/src/components/Scotsoun.astro
+++ b/src/components/Scotsoun.astro
@@ -5,14 +5,14 @@ import tComponent from '$i18n/translations/components/scotsoun';
import type Scotsoun from '$types/Scotsoun';
import PayPalButtons from './PayPalButtons.astro';
import relativePath from '$lib/relativePath';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
interface Props {
scotsoun: Scotsoun;
}
const { scotsoun } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/components/ScotsounCdGallery.astro b/src/components/ScotsounCdGallery.astro
index 40940a1..e1a9aaf 100644
--- a/src/components/ScotsounCdGallery.astro
+++ b/src/components/ScotsounCdGallery.astro
@@ -1,9 +1,9 @@
---
import { getAllScotsoun } from '$data/scotsoun';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import type Scotsoun from '$types/Scotsoun';
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const scotsoun = (await getAllScotsoun()).sort(scotsounIdDescending);
function scotsounIdDescending(scotsoun1: Scotsoun, scotsoun2: Scotsoun) {
diff --git a/src/components/SwitchLanguageLink.astro b/src/components/SwitchLanguageLink.astro
index d3e5ab9..fce633f 100644
--- a/src/components/SwitchLanguageLink.astro
+++ b/src/components/SwitchLanguageLink.astro
@@ -3,10 +3,10 @@ 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';
+import { getLocaleFromPathOrDefault } from '$lib/getLocaleFromPath';
// This logic assumes that there are exactly two locales.
-const currentLocale = getLocaleFromPath(Astro.url.pathname);
+const currentLocale = getLocaleFromPathOrDefault(Astro.url.pathname);
const otherLocale: Locale = currentLocale === 'sco' ? 'en-GB' : 'sco';
const currentPath = Astro.url.pathname;
const currentLocaleInPath = new RegExp(`(?<=^/)(${currentLocale}/)?`);
diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro
index c00570d..d5405aa 100644
--- a/src/layouts/Layout.astro
+++ b/src/layouts/Layout.astro
@@ -4,14 +4,14 @@ import Navbar from '$components/Navbar/Navbar.astro';
import translate from '$i18n/translate';
import tFeed from '$i18n/translations/pages/rss.xml';
import site from '$i18n/translations/site';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrDefault } from '$lib/getLocaleFromPath';
interface Props {
title: string;
}
const { title } = Astro.props;
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrDefault(Astro.url.pathname);
const t = translate(locale);
---
diff --git a/src/lib/getLocaleFromPath.ts b/src/lib/getLocaleFromPath.ts
index f5dc697..2cfaf41 100644
--- a/src/lib/getLocaleFromPath.ts
+++ b/src/lib/getLocaleFromPath.ts
@@ -1,11 +1,23 @@
-import locales from '$i18n/locales';
+import locales, { defaultLocale } from '$i18n/locales';
import type Locale from '$types/Locale';
-export default function (path: string): Locale {
+export function getLocaleFromPath(path: string): Locale | null {
const regex = new RegExp(`^/(${locales.join('|')})`);
const match = path.match(regex)?.[1];
if (!match) {
- throw new Error(`Cannot find locale in path ${path}.`);
+ return null;
}
return match as Locale;
}
+
+export function getLocaleFromPathOrDefault(path: string): Locale {
+ return getLocaleFromPath(path) ?? defaultLocale;
+}
+
+export function getLocaleFromPathOrThrow(path: string): Locale {
+ const locale = getLocaleFromPath(path);
+ if (!locale) {
+ throw new Error(`Cannot find locale in path ${path}.`);
+ }
+ return locale;
+}
diff --git a/src/pages/[locale]/news/index.astro b/src/pages/[locale]/news/index.astro
index 6e95884..37e7dd6 100644
--- a/src/pages/[locale]/news/index.astro
+++ b/src/pages/[locale]/news/index.astro
@@ -2,14 +2,14 @@
import translate from '$i18n/translate';
import tPage from '$i18n/translations/pages/news';
import Page from '$layouts/Page.astro';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import localeStaticPaths from '$lib/localeStaticPaths';
import { getCollection } from 'astro:content';
import { isNewsItemInLocale } from '$lib/newsUtils';
import NewsIndex from '$components/NewsIndex/NewsIndex.astro';
const allNews = await getCollection('news');
-const locale = getLocaleFromPath(Astro.url.pathname);
+const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
const allNewsInLocale = allNews.filter((item) => isNewsItemInLocale(item, locale));
const t = translate(locale);
diff --git a/src/pages/[locale]/rss.xml.ts b/src/pages/[locale]/rss.xml.ts
index b3cb22f..9d2e34b 100644
--- a/src/pages/[locale]/rss.xml.ts
+++ b/src/pages/[locale]/rss.xml.ts
@@ -2,7 +2,7 @@ import rss, { RSSFeedItem } from '@astrojs/rss';
import { CollectionEntry, getCollection } from 'astro:content';
import { getDate, getHref, isNewsItemInLocale } from '$lib/newsUtils';
import localeStaticPaths from '$lib/localeStaticPaths';
-import getLocaleFromPath from '$lib/getLocaleFromPath';
+import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import type { APIContext } from 'astro';
import translate from '$i18n/translate';
import tFeed from '$i18n/translations/pages/rss.xml';
@@ -13,7 +13,7 @@ import type Scotsoun from '$types/Scotsoun';
import { getAllScotsoun } from '$data/scotsoun';
export async function get(context: APIContext) {
- const locale = getLocaleFromPath(context.url.pathname);
+ const locale = getLocaleFromPathOrThrow(context.url.pathname);
const t = translate(locale);
const site = context.site;
if (!site) {