File size: 941 Bytes
0dbc9de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
---
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>