Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paraglide-Next: Fix paths segments that start with language tags being edited #3006

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/plenty-roses-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@inlang/paraglide-next": patch
---

fix issue where path-segments that start with a language tag confused the router.

Eg: `/entropy` would match the language `en` & be resolved to `/tropy`
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ describe("getCanonicalPath", () => {
expect(getCanonicalPath("/en/translated/1", "en")).toBe("/canonical-translated/1")
expect(getCanonicalPath("/de-CH/uebersetzt/1", "de-CH")).toBe("/canonical-translated/1")
})

it("does not get confused by a path that starts with a language tag", () => {
expect(getCanonicalPath("/de-something-else", "de")).toBe("/de-something-else")
})
})

describe("getLocalisedPath", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ export function PrefixStrategy<T extends string>({
// get the prefix for this language
const prefix = resolvedPrefixes[locale] ?? locale

const pathWithoutLocale: `/${string}` = localisedPath.startsWith(`/${prefix}`)
? ((localisedPath.replace(`/${prefix}`, "") || "/") as `/${string}`)
const pathWithoutLocale: `/${string}` = localisedPath.startsWith(`/${prefix}/`)
? (localisedPath.replace(`/${prefix}`, "") as `/${string}`)
: localisedPath === `/${prefix}`
? "/"
: localisedPath

for (const [canonicalPathDefinition, translationsForPath] of Object.entries(
Expand Down Expand Up @@ -166,7 +168,10 @@ export function PrefixStrategy<T extends string>({
const pathWithoutBase = request.nextUrl.pathname

const entries = Object.entries(resolvedPrefixes) as [T, string][]
const entry = entries.find(([, prefix]) => pathWithoutBase.startsWith(`/${prefix}`))
const entry = entries.find(
([, prefix]) =>
pathWithoutBase.startsWith(`/${prefix}/`) || pathWithoutBase === `/${prefix}`
)
const detected = entry?.[0]

// If no prefix is detected and prefixDefault is "never" -> use default language
Expand Down
Loading