|
1 | 1 | /** |
2 | 2 | * Language Toggle for SGEMM Optimization Docs |
3 | 3 | * Handles switching between English and Chinese pages |
| 4 | + * Auto-redirects based on browser language preference |
4 | 5 | */ |
5 | 6 |
|
6 | 7 | (function() { |
|
33 | 34 | 'zh-kernel-tensor-core': { en: '/docs/kernel-tensor-core/', 'zh-CN': '/zh/docs/kernel-tensor-core/' } |
34 | 35 | }; |
35 | 36 |
|
| 37 | + // Key for storing redirect state |
| 38 | + const REDIRECT_KEY = 'sgemm-lang-redirected'; |
| 39 | + |
36 | 40 | // Get base URL from Jekyll site |
37 | 41 | const baseurl = document.querySelector('meta[name="baseurl"]')?.content || |
38 | 42 | document.documentElement.getAttribute('data-baseurl') || |
39 | 43 | '/sgemm-optimization'; |
40 | 44 |
|
41 | 45 | /** |
42 | | - * Find the paired page path for a given page key and target language |
| 46 | + * Detect browser language preference |
| 47 | + * Returns 'zh-CN' for Chinese browsers, 'en' otherwise |
43 | 48 | */ |
44 | | - function findPairedPath(pageKey, targetLang) { |
45 | | - const pair = pagePairs[pageKey]; |
46 | | - if (pair && pair[targetLang]) { |
47 | | - return baseurl + pair[targetLang]; |
| 49 | + function detectBrowserLanguage() { |
| 50 | + const browserLang = navigator.language || navigator.userLanguage || 'en'; |
| 51 | + // Match zh-CN, zh-TW, zh-HK, zh-SG, or plain zh |
| 52 | + return /^zh/i.test(browserLang) ? 'zh-CN' : 'en'; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Check if this session has already handled language redirect |
| 57 | + */ |
| 58 | + function hasRedirectedThisSession() { |
| 59 | + try { |
| 60 | + return sessionStorage.getItem(REDIRECT_KEY) === 'true'; |
| 61 | + } catch (e) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Mark that redirect has been handled this session |
| 68 | + */ |
| 69 | + function markRedirectHandled() { |
| 70 | + try { |
| 71 | + sessionStorage.setItem(REDIRECT_KEY, 'true'); |
| 72 | + } catch (e) { |
| 73 | + // sessionStorage not available |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Check if user has explicitly set language preference |
| 79 | + */ |
| 80 | + function hasExplicitPreference() { |
| 81 | + try { |
| 82 | + return localStorage.getItem('preferred-doc-lang') !== null; |
| 83 | + } catch (e) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Auto-redirect based on browser language (first visit only) |
| 90 | + */ |
| 91 | + function autoRedirect() { |
| 92 | + // Skip if already redirected this session or user has explicit preference |
| 93 | + if (hasRedirectedThisSession() || hasExplicitPreference()) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // Mark that we've handled redirect for this session |
| 98 | + markRedirectHandled(); |
| 99 | + |
| 100 | + const browserLang = detectBrowserLanguage(); |
| 101 | + const currentPath = window.location.pathname; |
| 102 | + |
| 103 | + // Check if we're on an English page (not /zh/) |
| 104 | + const isEnglishPage = !currentPath.includes('/zh/'); |
| 105 | + const isHomePage = currentPath === baseurl + '/' || currentPath === baseurl; |
| 106 | + |
| 107 | + // Only redirect from English home page to Chinese if browser is Chinese |
| 108 | + if (isHomePage && browserLang === 'zh-CN') { |
| 109 | + window.location.replace(baseurl + '/zh/'); |
48 | 110 | } |
49 | | - // Fallback to language home |
50 | | - return targetLang === 'zh-CN' ? baseurl + '/zh/' : baseurl + '/'; |
51 | 111 | } |
52 | 112 |
|
53 | 113 | /** |
|
72 | 132 | } |
73 | 133 | } |
74 | 134 |
|
| 135 | + /** |
| 136 | + * Find the paired page path for a given page key and target language |
| 137 | + */ |
| 138 | + function findPairedPath(pageKey, targetLang) { |
| 139 | + const pair = pagePairs[pageKey]; |
| 140 | + if (pair && pair[targetLang]) { |
| 141 | + return baseurl + pair[targetLang]; |
| 142 | + } |
| 143 | + // Fallback to language home |
| 144 | + return targetLang === 'zh-CN' ? baseurl + '/zh/' : baseurl + '/'; |
| 145 | + } |
| 146 | + |
75 | 147 | /** |
76 | 148 | * Initialize language switcher functionality |
77 | 149 | */ |
78 | | - function init() { |
| 150 | + function initSwitcher() { |
79 | 151 | const switcher = document.querySelector('.language-switcher'); |
80 | 152 | if (!switcher) return; |
81 | 153 |
|
|
102 | 174 | }); |
103 | 175 | } |
104 | 176 |
|
| 177 | + /** |
| 178 | + * Initialize all language features |
| 179 | + */ |
| 180 | + function init() { |
| 181 | + // Run auto-redirect first (only on first visit) |
| 182 | + autoRedirect(); |
| 183 | + |
| 184 | + // Initialize switcher buttons |
| 185 | + initSwitcher(); |
| 186 | + } |
| 187 | + |
105 | 188 | // Initialize when DOM is ready |
106 | 189 | if (document.readyState === 'loading') { |
107 | 190 | document.addEventListener('DOMContentLoaded', init); |
|
0 commit comments