fix(web): persist docs language selection (#32551)

This commit is contained in:
Aiden Cline 2026-06-16 07:27:05 -04:00 committed by GitHub
parent 94652cfe4a
commit bd84c32860
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View File

@ -299,6 +299,7 @@ export default defineConfig({
Head: "./src/components/Head.astro",
Header: "./src/components/Header.astro",
Footer: "./src/components/Footer.astro",
LanguageSelect: "./src/components/LanguageSelect.astro",
SiteTitle: "./src/components/SiteTitle.astro",
},
plugins: [

View File

@ -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

View File

@ -0,0 +1,29 @@
---
import config from "virtual:starlight/user-config"
import StarlightLanguageSelect from "@astrojs/starlight/components/LanguageSelect.astro"
const locales = Object.keys(config.locales ?? {})
---
<opencode-language-select data-locales={JSON.stringify(locales)}>
<StarlightLanguageSelect />
</opencode-language-select>
<script>
document.addEventListener("change", (event) => {
if (!(event.target instanceof HTMLSelectElement)) return
const wrapper = event.target.closest("opencode-language-select")
if (!(wrapper instanceof HTMLElement)) return
const locales = JSON.parse(wrapper.dataset.locales ?? "[]") as string[]
const locale = locales[event.target.selectedIndex]
if (!locale) return
document.cookie = `oc_locale=${encodeURIComponent(locale === "root" ? "en" : locale)}; Path=/; Max-Age=31536000; SameSite=Lax`
})
</script>
<style>
opencode-language-select {
display: contents;
}
</style>