fix(web): persist docs language selection (#32551)
This commit is contained in:
parent
94652cfe4a
commit
bd84c32860
@ -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: [
|
||||
|
||||
@ -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
|
||||
|
||||
29
packages/web/src/components/LanguageSelect.astro
Normal file
29
packages/web/src/components/LanguageSelect.astro
Normal 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>
|
||||
Loading…
Reference in New Issue
Block a user