diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-13 20:41:29 +0100 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-13 20:41:29 +0100 |
| commit | 7781c69153e3956ca14684fe9a287fbbb8a01082 (patch) | |
| tree | 6bf18977ee93608aead54dc2322c0d42c2284f78 /src/lib/getLocaleFromPath.ts | |
| parent | 71eac2d383c9918645e99b6b845fb8f4faecf346 (diff) | |
Determines current locale with lib function
Diffstat (limited to 'src/lib/getLocaleFromPath.ts')
| -rw-r--r-- | src/lib/getLocaleFromPath.ts | 11 |
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; +} |
