Skip to content

Commit

Permalink
Merge pull request #2890 from opral/sveltekit-form-actions
Browse files Browse the repository at this point in the history
Fix Localisation of hrefs that are just params/hashes
  • Loading branch information
LorisSigrist authored Jun 5, 2024
2 parents 7a5c05e + 08a1e3f commit f9e978a
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 209 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-donkeys-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/paraglide-sveltekit": patch
---

Fix translation of hrefs that don't include a path. This is especially important with form actions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"devDependencies": {
"@inlang/paraglide-js": "workspace:*",
"@sveltejs/adapter-node": "^5.0.1",
"@sveltejs/adapter-static": "^3.0.0",
"@sveltejs/kit": "^2.4.3",
"@sveltejs/vite-plugin-svelte": "^3.0.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Actions } from "./$types"

export const prerender = false

export const actions: Actions = {
create: async ({ locals }) => {
console.info("create", locals.paraglide.lang)
},
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<script>
import { page } from "$app/stores"
import { enhance } from "$app/forms"
import { page } from "$app/stores"
import * as m from "$lib/paraglide/messages.js"
$: userId = Number.parseFloat($page.params.id);
</script>

<h1>{m.edit_user({ userId })}</h1>
<a href="/users/{userId}">{m.users()}</a>
<a href="/users/{userId}">{m.users()}</a>

<form method="POST" action="?/create" use:enhance>
<label for="name">Name</label>
<button>Submit</button>
</form>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adapter from "@sveltejs/adapter-static"
import adapter from "@sveltejs/adapter-node"
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte"

/** @type {import('@sveltejs/kit').Config} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@
function translateHref(href: string, hreflang: T | undefined): string {
try {
const from = new URL(get(page).url)
const original_to = new URL(href, new URL(from))
const localisedCurrentUrl = new URL(get(page).url)
const [localisedCurrentPath, suffix] = parseRoute(localisedCurrentUrl.pathname, absoluteBase)
const canonicalCurrentPath = i18n.strategy.getCanonicalPath(localisedCurrentPath, lang)
if (isExternal(original_to, from, absoluteBase) || i18n.config.exclude(original_to.pathname))
const canonicalCurrentUrl = new URL(localisedCurrentUrl)
canonicalCurrentUrl.pathname = serializeRoute(canonicalCurrentPath, absoluteBase, suffix)
const original_to = new URL(href, new URL(canonicalCurrentUrl))
if (isExternal(original_to, localisedCurrentUrl, absoluteBase) || i18n.config.exclude(original_to.pathname))
return href
const targetLanguage = hreflang ?? lang
Expand All @@ -71,7 +77,7 @@
dataSuffix
)
return getHrefBetween(from, to)
return getHrefBetween(localisedCurrentUrl, to)
} catch (error) {
if(dev) console.warn(`[paraglide-sveltekit] Failed to translate the link "${href}"`)
return href
Expand Down
Loading

0 comments on commit f9e978a

Please sign in to comment.