2024-10-09 21:30:05 +02:00
|
|
|
import type { AvailableLanguageTag } from "@/paraglide/runtime.js"
|
2024-10-20 22:25:52 +02:00
|
|
|
import type { AbsolutePathname, Project } from "@/types/types.ts"
|
2024-10-09 21:30:05 +02:00
|
|
|
|
2024-10-12 18:29:44 +02:00
|
|
|
interface TranslatedPathnames {
|
|
|
|
nb: AbsolutePathname
|
2024-10-20 11:01:54 +02:00
|
|
|
en: `/en${AbsolutePathname}`
|
2024-10-12 18:29:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 22:25:52 +02:00
|
|
|
export type NavLink =
|
|
|
|
"/"
|
|
|
|
| "/contact"
|
|
|
|
| "/projects"
|
|
|
|
| `/projects/${Project["id"]}`
|
|
|
|
| "/links"
|
2025-01-19 20:02:40 +01:00
|
|
|
| "/uses"
|
2024-10-12 18:29:44 +02:00
|
|
|
|
2024-10-20 11:01:54 +02:00
|
|
|
const paths: Set<NavLink> = new Set([
|
2024-10-12 18:29:44 +02:00
|
|
|
"/",
|
|
|
|
"/contact",
|
|
|
|
"/projects",
|
|
|
|
"/links",
|
2025-01-19 20:02:40 +01:00
|
|
|
"/uses"
|
2024-10-20 11:01:54 +02:00
|
|
|
])
|
2024-10-09 21:30:05 +02:00
|
|
|
|
2024-10-12 18:29:44 +02:00
|
|
|
/**
|
|
|
|
* Defines the localized pathnames for the site.
|
|
|
|
* The key must be used to navigate to the correct path.
|
|
|
|
* The value is the path that will be used for the given locale.
|
|
|
|
*
|
|
|
|
* @see https://inlang.com/m/iljlwzfs/paraglide-astro-i18n
|
|
|
|
*/
|
|
|
|
const pathnames: Record<AbsolutePathname, TranslatedPathnames> = {}
|
|
|
|
for (const path of paths) {
|
|
|
|
pathnames[path] = {
|
|
|
|
nb: path,
|
|
|
|
en: `/en${path}`
|
2024-10-09 21:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function localizePathname(
|
2024-10-12 18:29:44 +02:00
|
|
|
pathname: NavLink,
|
2024-10-09 21:30:05 +02:00
|
|
|
locale: AvailableLanguageTag
|
2024-10-20 22:25:52 +02:00
|
|
|
): string {
|
|
|
|
const pathnameParts = pathname.split("/")
|
|
|
|
const firstSegment: AbsolutePathname = `/${pathnameParts[1]}`
|
|
|
|
|
|
|
|
if (pathnames[firstSegment]) {
|
|
|
|
const localizedPathname = pathnames[firstSegment][locale]
|
|
|
|
|
|
|
|
const rest = pathnameParts.slice(2)
|
|
|
|
if (rest.length > 0) {
|
|
|
|
return `${localizedPathname}/${rest.join("/")}`
|
|
|
|
} else {
|
|
|
|
return localizedPathname
|
|
|
|
}
|
2024-10-09 21:30:05 +02:00
|
|
|
}
|
|
|
|
return pathname
|
|
|
|
}
|
2024-10-12 18:29:44 +02:00
|
|
|
|
|
|
|
export function resolvePathname(pathname: string): AbsolutePathname {
|
|
|
|
if (pathname.startsWith("/en")) {
|
|
|
|
return pathname.slice(3) as AbsolutePathname
|
|
|
|
}
|
|
|
|
return pathname as AbsolutePathname
|
|
|
|
}
|