From 7781c69153e3956ca14684fe9a287fbbb8a01082 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sun, 13 Aug 2023 20:41:29 +0100 Subject: Determines current locale with lib function --- src/lib/getLocaleFromPath.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/lib/getLocaleFromPath.ts (limited to 'src/lib') diff --git a/src/lib/getLocaleFromPath.ts b/src/lib/getLocaleFromPath.ts new file mode 100644 index 0000000..f5dc697 --- /dev/null +++ b/src/lib/getLocaleFromPath.ts @@ -0,0 +1,11 @@ +import locales from '$i18n/locales'; +import type Locale from '$types/Locale'; + +export default function (path: string): Locale { + const regex = new RegExp(`^/(${locales.join('|')})`); + const match = path.match(regex)?.[1]; + if (!match) { + throw new Error(`Cannot find locale in path ${path}.`); + } + return match as Locale; +} -- cgit v1.2.3