INNER CODE UNIT · TypeScript

localeFromPath

hsr88/mouzi · website/src/i18n/index.ts:9

export function localeFromPath(pathname: string): Locale {
  const candidate = pathname.split('/').filter(Boolean)[0]?.replace(/\.html$/, '') as Locale | undefined;
  return candidate && locales.includes(candidate) ? candidate : 'en';
}

export function stripLocale(pathname: string): string {
  const locale = localeFromPath(pathname);
  const cleanPath = pathname.replace(/\.html$/, '');
  if (locale === 'en') return cleanPath || '/';
  const stripped = cleanPath.replace(new RegExp(`^/${locale}(?=/|$)`), '');
  return stripped || '/';
}

export function localizedPath(pathname: string, locale: Locale): string {
  const path = stripLocale(pathname);
  return locale === 'en' ? path : `/${locale}${path === '/' ? '' : path}`;
}

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…