Merge c054cd6e18 into ae614d919f
commit
439aa95a8d
|
|
@ -298,6 +298,7 @@ export default defineConfig({
|
|||
Header: "./src/components/Header.astro",
|
||||
Footer: "./src/components/Footer.astro",
|
||||
SiteTitle: "./src/components/SiteTitle.astro",
|
||||
LanguageSelect: "./src/components/LanguageSelect.astro",
|
||||
},
|
||||
plugins: [
|
||||
theme({
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
import Default from '@astrojs/starlight/components/LanguageSelect.astro';
|
||||
---
|
||||
|
||||
<Default {...Astro.props}><slot /></Default>
|
||||
|
||||
<script>
|
||||
const LOCALE_MAP: Record<string, string> = {
|
||||
ar: "ar",
|
||||
bs: "bs",
|
||||
da: "da",
|
||||
de: "de",
|
||||
es: "es",
|
||||
fr: "fr",
|
||||
it: "it",
|
||||
ja: "ja",
|
||||
ko: "ko",
|
||||
nb: "no",
|
||||
pl: "pl",
|
||||
"pt-br": "br",
|
||||
ru: "ru",
|
||||
th: "th",
|
||||
tr: "tr",
|
||||
"zh-cn": "zh",
|
||||
"zh-tw": "zht",
|
||||
};
|
||||
|
||||
function cookieValueFromPath(pathname: string): string {
|
||||
const match = pathname.match(/^\/docs\/([^/]+)/);
|
||||
const segment = match?.[1]?.toLowerCase();
|
||||
if (segment && segment in LOCALE_MAP) {
|
||||
return LOCALE_MAP[segment]!;
|
||||
}
|
||||
return "en";
|
||||
}
|
||||
|
||||
document.querySelectorAll('starlight-lang-select select').forEach((select) => {
|
||||
select.addEventListener('change', (e) => {
|
||||
if (e.currentTarget instanceof HTMLSelectElement) {
|
||||
const locale = cookieValueFromPath(e.currentTarget.value);
|
||||
document.cookie = `oc_locale=${encodeURIComponent(locale)}; Path=/; Max-Age=31536000; SameSite=Lax`;
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Loading…
Reference in New Issue