2024-10-09 21:30:05 +02:00
|
|
|
import type { AvailableLanguageTag } from "@/paraglide/runtime.js"
|
2024-10-12 18:29:44 +02:00
|
|
|
import type { AbsolutePathname } from "@/types/types.ts"
|
2024-10-09 21:30:05 +02:00
|
|
|
|
2024-10-12 18:29:44 +02:00
|
|
|
interface TranslatedPathnames {
|
|
|
|
nb: AbsolutePathname
|
|
|
|
en: `/en${string}`
|
|
|
|
}
|
|
|
|
|
|
|
|
export type NavLink = "/" | "/contact" | "/projects" | "/links" | "/hardware"
|
|
|
|
|
|
|
|
const paths: NavLink[] = [
|
|
|
|
"/",
|
|
|
|
"/contact",
|
|
|
|
"/projects",
|
|
|
|
"/links",
|
|
|
|
"/hardware"
|
|
|
|
]
|
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
|
|
|
|
) {
|
|
|
|
if (pathnames[pathname]) {
|
|
|
|
return pathnames[pathname][locale]
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|