Start updating paraglide to v2

This commit is contained in:
2025-07-01 20:51:59 +02:00
parent c9cb20cd38
commit e5a95e0c97
12 changed files with 52 additions and 948 deletions

View File

@ -1,8 +1,9 @@
---
import { type NavLink, resolvePathname } from "@/utils/linking"
import { type NavLink } from "@/utils/linking"
import LocaleLink from "@/components/links/LocaleLink.astro"
import { deLocalizeHref } from "@/paraglide/runtime.js"
const pathname = resolvePathname(Astro.originPathname)
const pathname = deLocalizeHref(Astro.originPathname)
let paths: string[]
if (pathname === "/") {
@ -25,13 +26,13 @@ function getLink(path: string): NavLink {
{
paths.map((path, index) => (
<span>
{index != paths.length - 1 ? (
{ index != paths.length - 1 ? (
<span>
<LocaleLink to={getLink(path)}>{path}</LocaleLink>/
<LocaleLink to={ getLink(path) }>{ path }</LocaleLink>/
</span>
) : (
path
)}
) }
</span>
))
}

View File

@ -1,9 +1,10 @@
---
import LocaleLink from "./links/LocaleLink.astro"
import { type NavLink, resolvePathname } from "@/utils/linking"
import { type NavLink } from "@/utils/linking"
import { deLocalizeHref } from "@/paraglide/runtime"
const pathname = Astro.url.pathname
const currentPath = resolvePathname(pathname)
const currentPath = deLocalizeHref(pathname)
const isEnglish = pathname.startsWith("/en")
---

View File

@ -3,9 +3,9 @@ import Navbar from "./Navbar.astro"
import NavbarDrawer from "./NavbarDrawer.astro"
import HamburgerMenuButton from "./HamburgerMenuButton.astro"
import Breadcrumb from "../Breadcrumb.astro"
import { resolvePathname } from "@/utils/linking"
import { deLocalizeHref } from "@/paraglide/runtime"
const currentPath = `~${resolvePathname(Astro.originPathname)}`
const currentPath = `~${deLocalizeHref(Astro.originPathname)}`
const drawerToggleId = "header-drawer"
---

View File

@ -1,16 +1,16 @@
---
import { languageTag, type AvailableLanguageTag } from "@/paraglide/runtime"
import { localizePathname, type NavLink } from "@/utils/linking"
import { localizeHref, getLocale, type Locale } from "@/paraglide/runtime"
import { type NavLink } from "@/utils/linking"
import type { ComponentProps } from "@/types/props"
interface Props extends ComponentProps {
to: NavLink
lang?: AvailableLanguageTag
lang?: Locale
}
const { to, class: clazz, lang = languageTag() } = Astro.props
const { to, class: clazz, lang = getLocale() } = Astro.props
---
<a href={localizePathname(to, lang)} class={clazz}>
<a href={localizeHref(to, { locale: lang })} class={clazz}>
<slot />
</a>

View File

@ -3,7 +3,7 @@ import * as m from "@/paraglide/messages"
import Layout from "@/layouts/Layout.astro"
import BadgeList from "@/components/badge/BadgeList.astro"
import GiteaLink from "@/components/links/GiteaLink.astro"
import { languageTag } from "@/paraglide/runtime"
import { getLocale } from "@/paraglide/runtime"
import { getEntry, render } from "astro:content"
import { Image } from "astro:assets"
import dayjs from "dayjs"
@ -35,10 +35,10 @@ const {
function localeDateString(isoString: string): string {
let template = "DD-MM-YYYY"
if (languageTag() === "nb") {
if (getLocale() === "nb") {
template = "DD/MM/YYYY"
}
return dayjs(isoString).locale(languageTag()).format(template)
return dayjs(isoString).locale(getLocale()).format(template)
}
---

View File

@ -2,7 +2,7 @@
import Footer from "@/components/Footer.astro"
import Header from "@/components/header/Header.astro"
import Breadcrumb from "@/components/Breadcrumb.astro"
import { languageTag } from "@/paraglide/runtime"
import { getLocale } from "@/paraglide/runtime"
interface Props {
title: string
@ -16,7 +16,7 @@ const mainClass =
---
<!doctype html>
<html lang={languageTag()} dir={"ltr"}>
<html lang={getLocale()} dir={"ltr"}>
<head>
<meta charset="UTF-8" />
<meta name="author" content="Martin Berg Alstad" />

View File

@ -1,4 +1,3 @@
import type { AvailableLanguageTag } from "@/paraglide/runtime.js"
import type { AbsolutePathname, Project } from "@/types/types.ts"
interface TranslatedPathnames {
@ -22,8 +21,6 @@ const paths: Set<NavLink> = new Set([
"/uses",
])
const projectPaths: Set<string> = new Set<string>(["homepage", "sb1budget"])
/**
* Defines the localized pathnames for the site.
* The key must be used to navigate to the correct path.
@ -38,49 +35,3 @@ for (const path of paths) {
en: `/en${path}`,
}
}
export function localizePathname(
pathname: NavLink,
locale: AvailableLanguageTag,
): 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("/")}`
}
return localizedPathname
}
return pathname
}
export function resolvePathname(pathname: string): AbsolutePathname {
if (pathname.startsWith("/en")) {
return pathname.slice(3) as AbsolutePathname
}
return pathname as AbsolutePathname
}
export function isAbsolutePathname(path: string): path is AbsolutePathname {
return path.startsWith("/")
}
export function isNavLink(path: string): path is NavLink {
let basePath = path
if (path.startsWith("/en")) {
basePath = path.slice(2)
}
if (paths.has(basePath as NavLink)) {
return true
}
const pathSplit = basePath.split("/").slice(1)
return (
pathSplit.length === 2 &&
pathSplit[0] === "projects" &&
projectPaths.has(pathSplit[1])
)
}