SSR and i18n

Signed-off-by: Martin Berg Alstad <git@martials.no>
This commit is contained in:
2024-10-12 18:29:44 +02:00
parent 1a2fec6a59
commit 740cba625d
20 changed files with 273 additions and 62 deletions

View File

@ -1,20 +1,38 @@
import type { AvailableLanguageTag } from "@/paraglide/runtime.js"
import type { AbsolutePathname } from "@/types/types.ts"
type AbsolutePathname = `/${string}`
interface TranslatedPathnames {
nb: AbsolutePathname
en: `/en${string}`
}
// TODO what?
// https://inlang.com/m/iljlwzfs/paraglide-astro-i18n
const pathnames: Record<AbsolutePathname,
Record<AvailableLanguageTag, AbsolutePathname>
> = {
"/contact-me": {
nb: "/contact-me",
en: "/en/contact-me"
export type NavLink = "/" | "/contact" | "/projects" | "/links" | "/hardware"
const paths: NavLink[] = [
"/",
"/contact",
"/projects",
"/links",
"/hardware"
]
/**
* 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}`
}
}
export function localizePathname(
pathname: AbsolutePathname,
pathname: NavLink,
locale: AvailableLanguageTag
) {
if (pathnames[pathname]) {
@ -22,3 +40,10 @@ export function localizePathname(
}
return pathname
}
export function resolvePathname(pathname: string): AbsolutePathname {
if (pathname.startsWith("/en")) {
return pathname.slice(3) as AbsolutePathname
}
return pathname as AbsolutePathname
}