Skip to content
Closed
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
2 changes: 1 addition & 1 deletion packages/web/src/components/Footer.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import config from "virtual:starlight/user-config"
import LanguageSelect from "@astrojs/starlight/components/LanguageSelect.astro"
import LanguageSelect from "./LanguageSelect.astro"
import { Icon } from "@astrojs/starlight/components"

const { lang, editUrl, lastUpdated, entry } = Astro.locals.starlightRoute
Expand Down
17 changes: 17 additions & 0 deletions packages/web/src/components/LanguageSelect.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import Default from "@astrojs/starlight/components/LanguageSelect.astro"
---

<Default />

<script>
const select = document.querySelector("starlight-lang-select select") as HTMLSelectElement | null
if (select) {
select.addEventListener("change", (e) => {
const target = e.currentTarget as HTMLSelectElement
const path = target.value
const locale = path === "/docs/" ? "en" : path.match(/\/docs\/([^/]+)/)?.[1] || "en"
document.cookie = `oc_locale=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
})
}
</script>
14 changes: 9 additions & 5 deletions packages/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ export const onRequest = defineMiddleware((ctx, next) => {

if (ctx.url.pathname !== "/docs" && ctx.url.pathname !== "/docs/") return next()

const locale =
localeFromCookie(ctx.request.headers.get("cookie")) ??
localeFromAcceptLanguage(ctx.request.headers.get("accept-language"))
if (!locale || locale === "root") return next()
const cookieLocale = localeFromCookie(ctx.request.headers.get("cookie"))
if (cookieLocale !== null) {
if (cookieLocale === "root") return next()
return redirect(ctx.url, `/docs/${cookieLocale}/`)
}

const acceptLanguageLocale = localeFromAcceptLanguage(ctx.request.headers.get("accept-language"))
if (!acceptLanguageLocale || acceptLanguageLocale === "root") return next()

return redirect(ctx.url, `/docs/${locale}/`)
return redirect(ctx.url, `/docs/${acceptLanguageLocale}/`)
})
Loading