Files
xahau-web/src/utils/localizedHref.ts

38 lines
1.1 KiB
TypeScript

import { getRelativeLocaleUrl } from 'astro:i18n'
import { nonDefaultLocales } from '../i18n/locales'
function normalizePathname(pathname: string) {
if (!pathname || pathname === '/') return '/'
const normalized = pathname.replace(/\/+$/, '')
return normalized.startsWith('/') ? normalized : `/${normalized}`
}
function splitPath(path: string) {
const match = path.match(/^([^?#]*)(.*)$/)
return {
pathname: match?.[1] || '/',
suffix: match?.[2] || '',
}
}
export function stripLocalePrefix(pathname: string) {
const normalizedPathname = normalizePathname(pathname)
for (const locale of nonDefaultLocales) {
if (normalizedPathname === `/${locale}`) return '/'
if (normalizedPathname.startsWith(`/${locale}/`)) {
return normalizePathname(normalizedPathname.slice(locale.length + 1))
}
}
return normalizedPathname
}
export function getAlternateLocaleHref(
currentPathname: string,
targetLocale: string,
) {
const { pathname, suffix } = splitPath(currentPathname)
return `${getRelativeLocaleUrl(targetLocale, stripLocalePrefix(pathname))}${suffix}`
}