summaryrefslogtreecommitdiff
path: root/src/lib/getLocaleFromPath.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/getLocaleFromPath.ts')
-rw-r--r--src/lib/getLocaleFromPath.ts11
1 files changed, 11 insertions, 0 deletions
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;
+}