Skip to content

Commit

Permalink
Merge pull request #2469 from opral/parjs-47-astro-language-tag
Browse files Browse the repository at this point in the history
Fix PARJS-47 - Astro-adapter `languageTag()` not being set properly on windows
  • Loading branch information
LorisSigrist authored Mar 26, 2024
2 parents 833e632 + 365e5e3 commit 4b49e7f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-eagles-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/paraglide-js-adapter-astro": patch
---

fix: `languageTag()` not being set properly on windows. This bug was caused by duplicate module instantiation.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { paraglide } from "@inlang/paraglide-js-adapter-vite"
import path from "node:path"
import { alias } from "./alias.js"
import { fileURLToPath } from "node:url"
import { normalizePath } from "./utilts.js"

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand All @@ -23,6 +24,8 @@ export function integration(integrationConfig: {
entrypoint: middlewarePath,
})

const runtimePath = path.resolve(process.cwd(), integrationConfig.outdir, "runtime.js")

//Register the vite plugin
updateConfig({
vite: {
Expand All @@ -32,11 +35,10 @@ export function integration(integrationConfig: {
outdir: integrationConfig.outdir,
}),
alias({
"paraglide-js-adapter-astro:runtime": path.resolve(
process.cwd(),
integrationConfig.outdir,
"runtime.js"
),
//normalizing the path is very important!
//otherwise you get duplicate modules on windows
//learned that one the hard way (parjs-47)
"paraglide-js-adapter-astro:runtime": normalizePath(runtimePath),
}),
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import {
setLanguageTag,
sourceLanguageTag,
} from "paraglide-js-adapter-astro:runtime"
import type { MiddlewareHandler } from "astro"
import { type MiddlewareHandler } from "astro"

export const onRequest: MiddlewareHandler = async ({ url, locals, currentLocale }, next) => {
const locale = currentLocale ?? getLangFromPath(url.pathname)
const dir = guessTextDirection(locale)

setLanguageTag(locale)

locals.paraglide = {
lang: locale,
dir,
}

setLanguageTag(locale)
return await next()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// vendored in from vite
import path from "node:path"

const windowsSlashRE = /\\/g
function slash(p: string): string {
return p.replace(windowsSlashRE, "/")
}

const isWindows = typeof process !== "undefined" && process.platform === "win32"

export function normalizePath(id: string) {
return path.posix.normalize(isWindows ? slash(id) : id)
}

0 comments on commit 4b49e7f

Please sign in to comment.