summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2025-01-08 18:40:15 +0000
committerJoe Carstairs <me@joeac.net>2025-01-08 18:51:49 +0000
commit7e0219994b0f62b7d17310f5adebb549e129c952 (patch)
tree205afc5e3b411bd2ee84b83287307b2deb4d4192 /src
parenta63d28cf878197ead3e61eaf4acd1932a6edd94c (diff)
Fixes English news feed
Diffstat (limited to 'src')
-rw-r--r--src/lib/correctLocaleCasing.ts13
-rw-r--r--src/lib/getLocaleFromPath.ts5
-rw-r--r--src/lib/newsUtils.ts6
3 files changed, 20 insertions, 4 deletions
diff --git a/src/lib/correctLocaleCasing.ts b/src/lib/correctLocaleCasing.ts
new file mode 100644
index 0000000..4a0f669
--- /dev/null
+++ b/src/lib/correctLocaleCasing.ts
@@ -0,0 +1,13 @@
+import locales from '$i18n/locales';
+
+export function correctLocaleCasing(str: string) {
+ const strLowercase = str.toLowerCase();
+
+ for (const locale of locales) {
+ if (strLowercase === locale.toLowerCase()) {
+ return locale;
+ }
+ }
+
+ return str;
+}
diff --git a/src/lib/getLocaleFromPath.ts b/src/lib/getLocaleFromPath.ts
index 2cfaf41..b1e815c 100644
--- a/src/lib/getLocaleFromPath.ts
+++ b/src/lib/getLocaleFromPath.ts
@@ -1,13 +1,14 @@
import locales, { defaultLocale } from '$i18n/locales';
import type Locale from '$types/Locale';
+import { correctLocaleCasing } from './correctLocaleCasing';
export function getLocaleFromPath(path: string): Locale | null {
- const regex = new RegExp(`^/(${locales.join('|')})`);
+ const regex = new RegExp(`^/(${locales.join('|')})`, 'i');
const match = path.match(regex)?.[1];
if (!match) {
return null;
}
- return match as Locale;
+ return correctLocaleCasing(match) as Locale;
}
export function getLocaleFromPathOrDefault(path: string): Locale {
diff --git a/src/lib/newsUtils.ts b/src/lib/newsUtils.ts
index 233fdcf..4333534 100644
--- a/src/lib/newsUtils.ts
+++ b/src/lib/newsUtils.ts
@@ -1,6 +1,7 @@
import locales from '$i18n/locales';
import type Locale from '$types/Locale';
import { getCollection, type CollectionEntry } from 'astro:content';
+import { correctLocaleCasing } from './correctLocaleCasing';
export async function getMostRecentNewsItem(locale: Locale) {
const allNews = await getCollection('news');
@@ -14,7 +15,8 @@ export async function getMostRecentNewsItem(locale: Locale) {
export function getLocale(newsItem: CollectionEntry<'news'>) {
const srcPath = newsItem.id;
- return srcPath.slice(0, srcPath.indexOf('/'));
+ const localeInUnknownCase = srcPath.slice(0, srcPath.indexOf('/'));
+ return correctLocaleCasing(localeInUnknownCase);
}
export function getHref(newsItem: CollectionEntry<'news'>) {
@@ -46,7 +48,7 @@ export function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<
}
export function getDate(newsItem: CollectionEntry<'news'>) {
- const matchDate = new RegExp(`(${locales.join('|')})/(\\d\\d\\d\\d-\\d\\d-\\d\\d)`);
+ const matchDate = new RegExp(`(${locales.join('|')})/(\\d\\d\\d\\d-\\d\\d-\\d\\d)`, 'i');
const date = newsItem.id.match(matchDate)?.[2];
return date ? new Date(date) : null;
}