Skip to content

Commit 2aae299

Browse files
author
shijiashuai
committed
feat(docs): enhance browser language auto-detection
Improve automatic language switching based on browser preferences: - Use onBeforeMount for SSR compatibility with window check - Add localStorage to remember user's language preference - Save preference when user visits /en/ or /zh/ homepage - Check current path to avoid unnecessary redirects - Add missing link property to root locale config This ensures: 1. First-time visitors are redirected based on browser language 2. Returning visitors go to their previously selected language 3. No infinite redirect loops occur
1 parent 5e5d730 commit 2aae299

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export default withMermaid(defineConfig({
151151
root: {
152152
label: 'English',
153153
lang: 'en-US',
154+
link: '/en/',
154155
themeConfig: {
155156
nav: sharedNav('en'),
156157
sidebar: sharedSidebar['/en/'],

docs/en/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ hero:
1414
link: https://github.com/LessUp/compress-kit
1515
---
1616

17+
<script setup>
18+
import { onBeforeMount } from 'vue'
19+
20+
onBeforeMount(() => {
21+
if (typeof window !== 'undefined') {
22+
localStorage.setItem('docs-lang-preference', '/en/')
23+
}
24+
})
25+
</script>
26+
1727
## Quick Start
1828

1929
```bash

docs/index.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@ hero:
1414
---
1515

1616
<script setup>
17-
import { onMounted } from 'vue'
17+
import { onBeforeMount } from 'vue'
1818
import { useRouter } from 'vitepress'
1919

20-
onMounted(() => {
20+
// Client-only execution for SSR compatibility
21+
onBeforeMount(() => {
22+
if (typeof window === 'undefined') return
23+
2124
const router = useRouter()
22-
const lang = navigator.language || navigator.userLanguage
23-
if (lang.startsWith('zh')) {
25+
const userLang = navigator.language || navigator.userLanguage || ''
26+
const savedLang = localStorage.getItem('docs-lang-preference')
27+
28+
// Use saved preference if exists, otherwise use browser language
29+
const targetLang = savedLang || (userLang.startsWith('zh') ? '/zh/' : '/en/')
30+
const currentPath = window.location.pathname
31+
32+
// Only redirect if not already on correct language path
33+
if (targetLang === '/zh/' && !currentPath.startsWith('/zh')) {
2434
router.go('/zh/')
25-
} else {
35+
} else if (targetLang === '/en/' && !currentPath.startsWith('/en')) {
2636
router.go('/en/')
2737
}
2838
})

docs/zh/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ hero:
1414
link: https://github.com/LessUp/compress-kit
1515
---
1616

17+
<script setup>
18+
import { onBeforeMount } from 'vue'
19+
20+
onBeforeMount(() => {
21+
if (typeof window !== 'undefined') {
22+
localStorage.setItem('docs-lang-preference', '/zh/')
23+
}
24+
})
25+
</script>
26+
1727
## 快速开始
1828

1929
```bash

0 commit comments

Comments
 (0)