summaryrefslogtreecommitdiff
path: root/src/lib/getLocaleFromPath.ts
blob: f5dc697fbac5266efdafb314b7129f0ccc17eaa3 (plain)
1
2
3
4
5
6
7
8
9
10
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;
}