|
| 1 | +/** |
| 2 | + * Docusaurus Client Module for Language Detection |
| 3 | + * This module runs on the client side and handles browser language detection |
| 4 | + */ |
| 5 | +export function onRouteDidUpdate({ location, previousLocation }) { |
| 6 | + // Only run on root path |
| 7 | + if (location.pathname !== '/' && location.pathname !== '/index.html') { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + // Check if user has an existing locale preference (manual language selection) |
| 12 | + const savedLocale = localStorage.getItem('docusaurus.locale'); |
| 13 | + if (savedLocale === 'en') { |
| 14 | + // User explicitly chose English, respect it and don't redirect |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + if (savedLocale === 'zh-CN') { |
| 19 | + // User chose Chinese, no need to redirect |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + // Check if this is the first visit to the root path in this session |
| 24 | + if (sessionStorage.getItem('clientLangDetectionDone')) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + // Detect browser language |
| 29 | + const browserLang = navigator.language || navigator.userLanguage || ''; |
| 30 | + |
| 31 | + // Redirect Chinese users to Chinese version |
| 32 | + if (browserLang.toLowerCase().startsWith('zh')) { |
| 33 | + // Mark as done before redirecting to prevent loops |
| 34 | + sessionStorage.setItem('clientLangDetectionDone', '1'); |
| 35 | + // Redirect Chinese users to Chinese version |
| 36 | + window.location.replace('/zh-CN/'); |
| 37 | + } else { |
| 38 | + // Mark as done for non-Chinese users too |
| 39 | + sessionStorage.setItem('clientLangDetectionDone', '1'); |
| 40 | + } |
| 41 | +} |
0 commit comments